简体   繁体   English

使用executeScript和insertCSS的几个错误

[英]Several errors using executeScript and insertCSS

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 / firefox网络扩展正常运行,但是在firefox调试器中,它给出了此错误(在后台脚本中):“ 错误:尝试运行时脚本返回了非结构化可克隆数据

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 另外,我收到错误“ [Exception ...”,组件返回失败代码:0x80070057(NS_ERROR_ILLEGAL_VALUE)[nsIDOMWindowUtils.loadSheetUsingURIString]” nsresult:“ 0x80070057(NS_ERROR_ILLEGAL_VALUE)”位置:“ JS框架::资源:// gre / modules /ExtensionUtils.jsm :: runSafeSyncWithoutClone ::行60“数据:否] ”与行

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.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 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 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". 整个插件的作用是将“ comments”一词替换为“ bugs”。 The insertCSS ensures that the page will not be seen until the word-replacing occurs. insertCSS确保在替换单词之前看不到页面。 Both executeScript and insertCSS give an error, mentioned at the top, although it doesn't seem to have an effect on the result. 尽管顶部看起来没有影响,但executeScript和insertCSS都给出了错误,在顶部有提及。

Should I worry about the errors or leave them alone? 我应该担心这些错误还是让他们一个人呆着?

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. 我不确定是否已经解决所有问题,但是对于第二个问题,您似乎尝试将CS​​S插入到已经插入的页面中。 I ran into this issue once. 我曾经遇到过这个问题。 After I put chrome.tabs.removeCSS before re-insert it, the second error went away. 在重新插入chrome.tabs.removeCSS之后,第二个错误消失了。

For your first error, add true or undefined on a new line at the end of content.js 对于第一个错误,在content.js末尾的新行中添加trueundefined
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 executeScript 返回内容脚本的最后一个评估语句 -> https://developer.mozilla.org/zh-CN/Add-ons/WebExtensions/API/tabs/executeScript#Return_value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM