开发Google Chrome扩展程序时,未定义ReferenceError:未定义XXX

时间:2020-01-16 11:48:15

标签: javascript jquery google-chrome-extension

我正在开发Chrome插件,未定义XXX ...新手错误...

我的清单

"background": { "scripts": ["js/bg.js"] },
"content_scripts": [{
    "matches": [
        "https://XXXXXX.com/*"
    ],
    "js": ["js/jquery.js", "js/contentParser.js"]

然后是我的contentParser(只是一部分):

function ContentParser(app){

    this.loaded = 0;
    this.setting = null;
    this.clicking = false;
    this.selectedLanguages = false;
    this.click = function(object){
        var clickEvent = document.createEvent ("MouseEvent");
        clickEvent.initMouseEvent (
            "click", true, true, window, 0, 
            0, 0, 0, 0, 
            false, false, false, false, 
            0, null
        );
        object.dispatchEvent (clickEvent);
    };
    var self = this;

    $.get(chrome.runtime.getURL('setting.json'), null, function(data){
        self.setting = data;
    }, 'json');
}

我的bg.js

    function checkForValidUrl(tabId, changeInfo, tab) {
        if (tab.url.indexOf('https://xxxxx.xxxxxxxx.xxx') > -1)  {
            chrome.pageAction.show(tabId);
        }
    }

    chrome.pageAction.onClicked.addListener(function(tab) {
        chrome.tabs.executeScript(null, {file: "js/translate.js"});
    });

    chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
        checkForValidUrl(tabId, changeInfo, tab);

        if(changeInfo.status === 'complete'){
            chrome.tabs.executeScript(null, {file: "js/init.js"});
        }
    });

然后错误出现在我的init.js上,ContentParser带有下划线:

未捕获的ReferenceError:未定义ContentParser

if(typeof document.behouse != 'object'){
    alert ('creem objecte');

    document.contentParser = new ContentParser(this);  // ERROR HERE
    document.contentParser.loaded++;

}

正如您所看到的,ContentParser.js已经在content_scripts中,我在做什么错了?

0 个答案:

没有答案