Skip to content

web: Fix extension webpack import not working (close #10981) #19664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion web/packages/core/src/current-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ try {
"src" in document.currentScript &&
document.currentScript.src !== ""
) {
let src = document.currentScript.src;
let src = getScriptOriginalSrc(document.currentScript);

// CDNs allow omitting the filename. If it's omitted, append a slash to
// prevent the last component from being dropped.
Expand All @@ -24,3 +24,31 @@ try {
} catch (_e) {
console.warn("Unable to get currentScript URL");
}

/**
*
* Obtain the origin src content according to the running environment of the \<script\> node
*
* @param script \<script\> node instance
*
* @returns \<script\> node origin src
*/
function getScriptOriginalSrc(script: HTMLScriptElement): string {
const scriptUrl = script.src;
const scriptUrlPolyfill = script.getAttribute("ruffle-src-polyfill");
// Reset webkit mask url should be safe
if (scriptUrlPolyfill && "webkit-masked-url://hidden/" === scriptUrl) {
try {
const currentPolyfillURL = new URL(".", scriptUrlPolyfill);
const isExtensionUrl =
currentPolyfillURL.protocol.includes("extension");
// Only apply to extension
if (isExtensionUrl) {
return scriptUrlPolyfill;
}
} catch {
// Fallback to itself src
}
}
return scriptUrl;
}
3 changes: 3 additions & 0 deletions web/packages/extension/src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ function injectScriptURL(url: string): Promise<void> {
script.src = url;
// safari 16+ script.src will be masked to "webkit-masked-url://hidden/"
script.setAttribute("ruffle-id", String(ID));
if (url !== script.src) {
script.setAttribute("ruffle-src-polyfill", url);
}
(document.head || document.documentElement).append(script);
return promise;
}
Expand Down
8 changes: 8 additions & 0 deletions web/packages/extension/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare global {
/**
* https://webpack.js.org/guides/public-path/#on-the-fly
*/
let __webpack_public_path__: string;
}

export {};
45 changes: 42 additions & 3 deletions web/packages/extension/src/ruffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ function handleMessage(message: Message) {
}

let ID: string | null = null;
if (document.currentScript !== undefined && document.currentScript !== null) {
if ("src" in document.currentScript && document.currentScript.src !== "") {
if (document.currentScript instanceof HTMLScriptElement) {
polyfillCurrentScriptWebpackPublicPath();
if (document.currentScript.src) {
try {
ID = new URL(document.currentScript.src).searchParams.get("id");
} catch (_) {
// ID remains null.
}
}
if (ID === null) {
// if `script.src` is masked, get id from attrs
// Fallback to get id from attrs
const ruffleId = document.currentScript.getAttribute("ruffle-id");
if (ruffleId) {
ID = ruffleId;
Expand All @@ -57,6 +58,44 @@ function openInNewTab(swf: URL): void {
window.postMessage(message, "*");
}

/**
* We are overriding the publicPath automatically configured by webpack here because the browser might mask the script's src attribute (for example, Safari 16+ may mask the script.src of an extension as webkit-masked-url://hidden/).
*/
function polyfillCurrentScriptWebpackPublicPath(): void {
const script = document.currentScript as HTMLScriptElement;
try {
const scriptUrlPolyfill = script.getAttribute("ruffle-src-polyfill");
if (!scriptUrlPolyfill) {
return;
}
const scriptUrl = script.src;
const scriptAutoPublicPath =
getWebpackPublicPathFromScriptSrc(scriptUrl);

if (
scriptUrl === "webkit-masked-url://hidden/" &&
__webpack_public_path__ === scriptAutoPublicPath
) {
const polyfillPath =
getWebpackPublicPathFromScriptSrc(scriptUrlPolyfill);
// TODO: If there are other scripts that need to be dynamically created and executed, the current polyfill logic should also be applied.
__webpack_public_path__ = polyfillPath;
}
// TODO: Process other situations when mask url not webkit-masked-url://hidden/
} catch (_) {
// Continue to run
}
}

// Copied from Webpack: https://github.com/webpack/webpack/blob/f1bdec5cc70236083e45b665831d5d79d6485db7/lib/runtime/AutoPublicPathRuntimeModule.js#L75
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This link may be confusing.
It actually comes from webpack automatic publicPath. Specifically refers to get publicPath from the currentScript.src.

function getWebpackPublicPathFromScriptSrc(scriptUrl: string): string {
return scriptUrl
.replace(/^blob:/, "")
.replace(/#.*$/, "")
.replace(/\?.*$/, "")
.replace(/\/[^/]+$/, "/");
}

if (ID) {
window.addEventListener("message", (event) => {
// We only accept messages from ourselves.
Expand Down