I want to inject script from firefox web extension to tabId thought browser.tabs.executeScript API.
I have a file Browser.js
MyFunc.Browser = (function() {
var self;
function Browser() {
self = this;
}
Browser.getExtensionURI = function() {
return "chrome-extension://";
};
return Browser;
})();
And execute script function:
var executing = browser.tabs.executeScript(tabId, {
file: "js/contentscripts/Browser.js"
});
executing.then(function(results) {
console.log("url: " + tabUrl + ", result", results);
}, function(error) {
return console.log("Inject scripts error: " + error);
});
But script cannot inject to tab and show error.
How I can fix it?
Manifest file:
{
"name": "abc",
"background": {
"page": "background.html"
},
"browser_action": {
"default_icon": "icons/icon_19.png",
"default_popup": "login.html",
},
"content_scripts": [
{
"web_accessible_resources": [
"js/contentscripts/Browser.js",
],
"js": [
"js/contentscripts/ContentScript.js"
],
"matches": [
"file://*/*",
"http://*/*",
"https://*/*"
],
"run_at": "document_end",
"all_frames": true
},
{
"js": [
"js/contentscripts/Browser.js",
],
"matches": [
"file://*/*",
"http://*/*",
"https://*/*"
],
"run_at": "document_start",
"all_frames": true
}
],
"icons": {
"16": "icons/icon_16.png",
"19": "icons/icon_19.png"
},
"incognito": "spanning",
"permissions": [
"activeTab",
"tabs",
"http://*/*",
"https://*/*",
"<all_urls>"
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"version": "1.1.16"
}
in "js/contentscripts/Browser.js" file, add "undefined;" to last line.
the value will return to result of "executing.then" first callback argument
reference:https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/executeScript#Return_value
I think I understand your issue now.
The return data of your executeScript has to be structured clonable.
In order to be considered structured clonable the data has to match one of these data types:
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#Supported_types
In your case the problem is that you are returning an object that has functions in it's properties. Thus it is non-structured-clonable, which explains your error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With