Several errors using executeScript and insertCSS

0

The chrome/firefox web extension I am developing works properly, but in the firefox debugger, it gives this error (in the background script): "Error: Script returned non-structured-clonable data" when attempting to run

chrome.tabs.executeScript({
    file: "jquery-3.1.1.js",
    runAt: 'document_end'
});
chrome.tabs.executeScript({
    file: "findAndReplaceDOMText.js",
    runAt: 'document_end'
});
chrome.tabs.executeScript({
    file: "content.js",
    runAt: 'document_end'
});

Also, I get the error "[Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIDOMWindowUtils.loadSheetUsingURIString]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: resource://gre/modules/ExtensionUtils.jsm :: runSafeSyncWithoutClone :: line 60" data: no]" with the line

chrome.tabs.insertCSS({
    code: "html { visibility:hidden; }",
    runAt: "document_end"
});

For the first issue, I am lost, and for the second, I do not know what the illegal value may be.

manifest.json

{
"manifest_version": 2,
"name": "Error Example",
"version": "0.1",

"description": "Replace a word.",
"icons": {
    "48": "icons/black-48.jpg"
},

"permissions": [
    "<all_urls>",
    "activeTab",
    "tabs",
    "webNavigation"
],

"background": {
    "scripts": ["background.js"]
}
}

background.js

var latestList = [];

function run_scripts(whenToRun) {
chrome.tabs.executeScript({
    file: "jquery-3.1.1.js",
    runAt: whenToRun
});
chrome.tabs.executeScript({
    file: "findAndReplaceDOMText.js",
    runAt: whenToRun
});
chrome.tabs.executeScript({
    file: "content.js",
    runAt: whenToRun
});
}

function onBeforeNavigate(details) {
chrome.tabs.insertCSS({
    code: "html { visibility:hidden; }",
    runAt: "document_end"
});
run_scripts("document_end");
}

chrome.webNavigation.onBeforeNavigate.addListener(onBeforeNavigate);

content.js

$(document).ready(function() {

var words_to_replace = [];

function replaceWord() {

    var word1 = "comments";
    var word2 = "bugs";

    reWord1 = new RegExp(word1, "gi");

    findAndReplaceDOMText(document.getElementsByTagName('body')[0], {
        preset: 'prose',
        find: reWord1,
        replace: word2
    });
    $("html").css("visibility", 'visible');
}
replaceWord();
});

What the whole addon does is replace the word "comments" with "bugs". The insertCSS ensures that the page will not be seen until the word-replacing occurs. Both executeScript and insertCSS give an error, mentioned at the top, although it doesn't seem to have an effect on the result.

Should I worry about the errors or leave them alone?

javascript
google-chrome-extension
firefox-webextensions
asked on Stack Overflow Feb 26, 2017 by Tritofic • edited Feb 26, 2017 by Tritofic

2 Answers

0

I am not sure if you already get all fixed up but for the second issue, it looks like you try to insertCSS to the already-inserted page. I ran into this issue once. After I put chrome.tabs.removeCSS before re-insert it, the second error went away.

answered on Stack Overflow Apr 18, 2017 by piptan
0

For your first error, add true or undefined on a new line at the end of content.js
This way you will only get real errors jump to the error promise

Reason
executeScript returns the last evaluated statement of a content script -> https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/executeScript#Return_value

answered on Stack Overflow Jan 10, 2018 by e-motiv

User contributions licensed under CC BY-SA 3.0