-
Notifications
You must be signed in to change notification settings - Fork 215
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
Can't send runtime messages in Firefox; Error: "TypeError: promise.then is not a function" #105
Comments
Perhaps try changing this line: |
On second thoughts, that probably won't make any difference. It could be the way you're importing it? This might bypass the check intended to stop the polyfill being applied to Firefox? The main page has suggestions about how to import it. |
Has anyone found a solution to this problem, running into this as well. |
Can you share your generated bundle? Side note, the following is incorrect if your intent is to only respond to browser.runtime.onMessage.addListener(async ({ kind, data }) => {
if (kind === "LOREM") {
return Promise.resolve({ response: data });
}
return false;
}); should be without browser.runtime.onMessage.addListener(async ({ kind, data }) => {
if (kind === "LOREM") {
return { response: data };
}
}); |
@Rob--W I am trying to send a message from a content script, if I pause the debugger just before it is about to send the message, and I send a message that is not handled: I get the same error.
|
@rikedia Please share your generated code... |
@rikedia In your extension, the native The fact that replacing the global |
@Rob--W Thanks for pointing that out. I fixed my problems by removing babel-polyfill and adding babel-plugin-transform-runtime with the polyfill option set to false. .babelrc{
...
"plugins": [
...
[
"transform-runtime",
{
"polyfill": false,
"regenerator": true
}
]
]
} |
Thanks @Rob--W and @rikedia. That helped me get rid of the "promise.then is not a function" error but I'm now getting another error in Firefox:
(It works in Chrome) The generated files are available in this gist. And here is an excerpt with my function from var sendMessage = function () {
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
var _ref2, response;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return _webextensionPolyfill2.default.runtime.sendMessage({
kind: "LOREM",
data: "ipsum"
});
case 2:
_ref2 = _context.sent;
response = _ref2.response;
console.log("content_script: response:", response);
case 5:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
return function sendMessage() {
return _ref.apply(this, arguments);
};
}(); // content_script.js I've added the transform-runtime to my webpack config and removed the babel-polyfill: const path = require("path");
module.exports = {
entry: {
background_page: ["./src/background_page.js"],
content_script: ["./src/content_script.js"]
},
output: {
path: path.resolve(__dirname + "/dist"),
filename: "[name].js"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: "babel-loader",
options: {
presets: ["babel-preset-env"],
plugins: [
[
"babel-plugin-transform-runtime",
{ polyfill: false, regenerator: true }
]
]
}
}
}
]
}
}; |
Have you tried following the suggestion from #105 (comment) ? |
I got it working by changing my webpack setup to use "babel-preset-env" with my current node as the target so that it uses native async/await. Thanks @Rob--W for the help! My updated webpack config for completeness: const path = require("path");
module.exports = {
entry: {
background_page: ["./src/background_page.js"],
content_script: ["./src/content_script.js"]
},
output: {
path: path.resolve(__dirname + "/dist"),
filename: "[name].js"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: "babel-loader",
options: {
presets: [["babel-preset-env", { targets: { node: "current" } }]]
}
}
}
]
}
}; |
Closing this since this is not an issue with the polyfill, but with Firefox * as explained in #105 (comment) . * The polyfill is a no-op in Firefox. We will document this more clearly and close #55 in the future. |
@Rob--W I was also facing similar issue in Firefox. |
@abhijithvijayan - Just had the exact same issue. Took a few hours to get to this thread and this point. My
|
@Rob--W I think that it may be reasonable to explicitly document the "TypeError: promise.then is not a function" error in one of the following sections of the README.md file e.g.:
even if this isn't technically a issue of the webextension-polyfill, so that an addon developer that has this issue doesn't need to find this closed issue to know how to fix it. |
Let's update the docs. Preferably this should be fixed in Firefox. The validation in the extension framework is stricter than the JS engine's (i.e. in situations where promises are used, thenables are perfectly fine, whereas in the extension framework we don't accept them currently). |
This prepares the project for adding other SCMs. The changes to Babel were due to an issue with shimming Promises and Firefox (mozilla/webextension-polyfill#105 (comment))
This prepares the project for adding other SCMs. The changes to Babel were due to an issue with shimming Promises and Firefox (mozilla/webextension-polyfill#105 (comment))
I'm getting the following error in Firefox when I try to send a runtime message from a content script to a background script:
I'm using webpack with babel-polyfill to include the generator runtime:
It works fine in Chrome. What am I missing?
The text was updated successfully, but these errors were encountered: