使用 Polyfill 将 Firefox 扩展移植到 Chrome 时未捕获(承诺)错误
Uncaught (in Promise) Error When Using Polyfill for Porting Firefox Extensions to Chrome
我为 Firefox Quantum 写了一个网络扩展。该扩展程序有一个弹出窗口,单击它会提供一个 3 按钮菜单。单击其中一个按钮时,扩展程序会将包含 iFrame 的 DIV 注入用户的当前页面。
Firefox 扩展使用 promise
API 并使用 browser
命名空间而不是 "callbacks" 和 chrome
命名空间。因此,Mozilla 提供了一个 polyfill 来允许轻松移植为 Firefox Extensions 编写的代码。
摘自this article。
If you do write your extension to use browser
and promises, then Firefox also provides a polyfill that will enable it to run in Chrome: https://github.com/mozilla/webextension-polyfill.
我按照 github 上的教程修改了弹出窗口的 html 文件(我删除了 CSS 类 以使示例更简洁):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="https://fonts.googleapis.com/css?family=Nunito:800" rel="stylesheet">
<script type="application/javascript" src="../polyfills/browser-polyfill.js"></script>
</head>
<body>
<div style="display: flex; flex-direction: column; justify-content: space-evenly; align-items: center; width: 100vw; min-width: 200px;">
<button class="btn" id="3">Short Summary</button>
<button class="btn" id="5">Medium Summary</button>
<button class="btn" id="7">Long Summary</button>
<script src="choose_length_page.js"></script>
</div>
</body>
</html>
然后在我的 "choose_length_page.js" 文件中,我有以下代码:
//Enable the polyfill for the content script and execute it in the current tab
browser.tabs.executeScript({ file: "../polyfills/browser-polyfill.js" }).then(loadContentScript);
function loadContentScript() {
browser.tabs.executeScript({ file: "../inject-content/inject.js" }).then(listenForClicks);
}
function listenForClicks() {
document.addEventListener('click', e => {
if (!e.target.classList.contains('btn')) {
return;
} else {
browser.tabs.query({ active: true, currentWindow: true })
.then(tabs => {
browser.tabs.sendMessage(tabs[0].id, { summaryLength: e.target.id, targetURL: tabs[0].url });
});
}
});
}
然而,当我 运行 这段代码时,我得到以下错误("choose_length_page.html" 是这个 post 中的 HTML 文件):
为什么会出现此错误,我该如何解决?
更新 1 -
在wOxxOm的建议下,我尝试指定从根包限定的路径。我是这样做的:
<head>
<meta charset="utf-8">
<link href="https://fonts.googleapis.com/css?family=Nunito:800" rel="stylesheet">
<script type="application/javascript" src="chrome-extension://__MSG_@@extension_id__/polyfills/browser-polyfill.js"></script>
这样做之后,我收到以下消息:
[Deprecation] Subresource requests whose URLs contain embedded credentials (e.g. https://user:pass@host/
) are blocked. See https://www.chromestatus.com/feature/5669008342777856 for more details.
所以,我认为这行不通。
我设法修复了错误:
我将我的 javascript 文件更改为(我向 promise 添加了 catch 语句,并更改了路径以与 JaromandaX 的建议保持一致):
//Enable the polyfill for the content script and execute it in the current tab
browser.tabs.executeScript({ file: "/polyfills/browser-polyfill.js" }).then(loadContentScript).catch((error) => logError(error));
function loadContentScript() {
browser.tabs.executeScript({ file: "/inject-content/inject.js" }).then(listenForClicks).catch((error) => logError(error));
}
function listenForClicks() {
document.addEventListener('click', e => {
if (!e.target.classList.contains('btn')) {
return;
} else {
browser.tabs.query({ active: true, currentWindow: true })
.then(tabs => {
browser.tabs.sendMessage(tabs[0].id, { summaryLength: e.target.id, targetURL: tabs[0].url });
});
}
});
}
function logError(error) {
console.log(error);
}
我还把script
标签的src
属性中的路径改成了:
<script type="application/javascript" src="/polyfills/browser-polyfill.js"></script>
错误似乎是我没有抓住承诺。
我为 Firefox Quantum 写了一个网络扩展。该扩展程序有一个弹出窗口,单击它会提供一个 3 按钮菜单。单击其中一个按钮时,扩展程序会将包含 iFrame 的 DIV 注入用户的当前页面。
Firefox 扩展使用 promise
API 并使用 browser
命名空间而不是 "callbacks" 和 chrome
命名空间。因此,Mozilla 提供了一个 polyfill 来允许轻松移植为 Firefox Extensions 编写的代码。
摘自this article。
If you do write your extension to use
browser
and promises, then Firefox also provides a polyfill that will enable it to run in Chrome: https://github.com/mozilla/webextension-polyfill.
我按照 github 上的教程修改了弹出窗口的 html 文件(我删除了 CSS 类 以使示例更简洁):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="https://fonts.googleapis.com/css?family=Nunito:800" rel="stylesheet">
<script type="application/javascript" src="../polyfills/browser-polyfill.js"></script>
</head>
<body>
<div style="display: flex; flex-direction: column; justify-content: space-evenly; align-items: center; width: 100vw; min-width: 200px;">
<button class="btn" id="3">Short Summary</button>
<button class="btn" id="5">Medium Summary</button>
<button class="btn" id="7">Long Summary</button>
<script src="choose_length_page.js"></script>
</div>
</body>
</html>
然后在我的 "choose_length_page.js" 文件中,我有以下代码:
//Enable the polyfill for the content script and execute it in the current tab
browser.tabs.executeScript({ file: "../polyfills/browser-polyfill.js" }).then(loadContentScript);
function loadContentScript() {
browser.tabs.executeScript({ file: "../inject-content/inject.js" }).then(listenForClicks);
}
function listenForClicks() {
document.addEventListener('click', e => {
if (!e.target.classList.contains('btn')) {
return;
} else {
browser.tabs.query({ active: true, currentWindow: true })
.then(tabs => {
browser.tabs.sendMessage(tabs[0].id, { summaryLength: e.target.id, targetURL: tabs[0].url });
});
}
});
}
然而,当我 运行 这段代码时,我得到以下错误("choose_length_page.html" 是这个 post 中的 HTML 文件):
为什么会出现此错误,我该如何解决?
更新 1 -
在wOxxOm的建议下,我尝试指定从根包限定的路径。我是这样做的:
<head>
<meta charset="utf-8">
<link href="https://fonts.googleapis.com/css?family=Nunito:800" rel="stylesheet">
<script type="application/javascript" src="chrome-extension://__MSG_@@extension_id__/polyfills/browser-polyfill.js"></script>
这样做之后,我收到以下消息:
[Deprecation] Subresource requests whose URLs contain embedded credentials (e.g.
https://user:pass@host/
) are blocked. See https://www.chromestatus.com/feature/5669008342777856 for more details.
所以,我认为这行不通。
我设法修复了错误:
我将我的 javascript 文件更改为(我向 promise 添加了 catch 语句,并更改了路径以与 JaromandaX 的建议保持一致):
//Enable the polyfill for the content script and execute it in the current tab
browser.tabs.executeScript({ file: "/polyfills/browser-polyfill.js" }).then(loadContentScript).catch((error) => logError(error));
function loadContentScript() {
browser.tabs.executeScript({ file: "/inject-content/inject.js" }).then(listenForClicks).catch((error) => logError(error));
}
function listenForClicks() {
document.addEventListener('click', e => {
if (!e.target.classList.contains('btn')) {
return;
} else {
browser.tabs.query({ active: true, currentWindow: true })
.then(tabs => {
browser.tabs.sendMessage(tabs[0].id, { summaryLength: e.target.id, targetURL: tabs[0].url });
});
}
});
}
function logError(error) {
console.log(error);
}
我还把script
标签的src
属性中的路径改成了:
<script type="application/javascript" src="/polyfills/browser-polyfill.js"></script>
错误似乎是我没有抓住承诺。