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?

Tritofic
  • 101
  • 1
  • 2
  • Please [edit] the question to be on-topic: include a [mcve] that duplicates the problem. For Chrome extensions or Firefox WebExtensions this usually means that you need to include your *manifest.json* and some of the background, content, and/or popup scripts/HTML. Questions seeking debugging help ("why isn't this code working the way I want?") must include: (1) the desired behavior, (2) a specific problem or error and (3) the shortest code necessary to reproduce it *in the question itself*. Please also see: [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Feb 26 '17 at 00:54
  • What version of Firefox did you test on? Are you sure your CSS is working? Did you test with, or without multiprocess enabled? I did quite a bit of testing wrt. using `executeScript()` (and a bit with `insertCSS()`) to inject at the earliest possible moment. At least for `executeScript()`, `onBeforeNavigate` was too soon (various issues, including the scripts just not injecting, errors in console, etc.; problems on both Firefox and Chrome). It was necessary to delay at least until the `webRequest.onSendHeaders` (Firefox) and `onHeadersReceived` (Chrome) in order to be consistently functional. – Makyen Feb 26 '17 at 02:19
  • I am using Firefox 51.0.1, the CSS does appear to be working because without it, the page initially says "comments" before "bugs", but with the CSS, the page starts as blank, and the word switch has already occurred when the page is made visible again. Using "browser.webRequest.onHeadersReceived.addListener( onSendHeaders, {urls: [targetPage]} ); " didn't appear to trigger anything, with targetPage = 'https://reddit.com', but I am not sure if I used it correctly. Using onDomContentLoaded only gave me a single error: "Error: Script returned non-structured-clonable data". – Tritofic Feb 26 '17 at 02:45
  • This is my current edit: `function onSendHeaders(e) { console.log("onSendHeader was triggered"); chrome.tabs.insertCSS({ code: "html { visibility:hidden; }", runAt: "document_end" }); run_scripts("document_end"); return { responseHeaders: e.responseHeaders }; } browser.webRequest.onHeadersReceived.addListener( onSendHeaders, { urls: [targetPage] } ); ` – Tritofic Feb 26 '17 at 02:47
  • Please include the code for *findAndReplaceDOMText.js* in the question. – Makyen Feb 26 '17 at 02:48
  • I'm not sure if you are injecting in every page as is shown in the question, but *please* don't load jQuery into **every** page unless you **need** to. jQuery is 85kiB of minimized code. This is a significant burden with which to saddle *every single page*. What of those of us who have 100's of tabs open? While It is possible you really *need* to load jQuery, what's in the question is it's there to save a couple hundred characters in your own code by not using vanilla JavaScript. If that is the case (we have no way to know), doing so is a *very* poor trade-off from your user's point of view. – Makyen Feb 26 '17 at 02:52
  • Here is findAndReplaceDOMText: https://github.com/padolsey/findAndReplaceDOMText/blob/master/src/findAndReplaceDOMText.js . Looking at it now, I really don't need jQuery and will remove it. – Tritofic Feb 26 '17 at 03:03
  • I appreciate the pointer to that repository. That's quite a bit of code for what it appears you are doing with `findAndReplaceDOMText()`. [This answer](http://stackoverflow.com/a/40576258/3773011) contains code that makes substitutions in DOM text. With very little effort, it could be reworked to accept the matches/substitutions as input. I'm biased, as I wrote that answer, but it is considerably less code for what you are doing. It's going to be a bit before I'm going to be able to take a detailed look at this, I'm trying to finish up something else. – Makyen Feb 26 '17 at 03:54

2 Answers2

1

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

e-motiv
  • 5,795
  • 5
  • 27
  • 28
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.

piptan
  • 65
  • 2
  • 11