Chrome扩展程序运行后台脚本,然后运行内容脚本

时间:2016-05-13 10:32:11

标签: google-chrome google-chrome-extension content-script

点击浏览器操作后,我执行注入脚本,注入几个嵌套的div

chrome.tabs.executeScript(tab.id, {
    file: 'src/injection.js'
});

在我单击浏览器操作之前,我的内容脚本运行并且不能正常工作,因为缺少了在浏览器操作上注入的div。是否有任何解决方案来重新加载内容脚本而不重新加载页面?

我找到了将我的内容脚本文件放入chrome.tabs.executeScript函数的建议,但它不起作用

更新

的manifest.json

{
    "background": {
        "page": "background/background.html",
         "persistent": true
    },
    "browser_action": {
        "default_icon": "img/favicon.png",
        "default_title": "CheapWatcher"
    },
    "content_scripts": [{
        "js": [
            "lib/jquery.min.js",
            "lib/jquery-ui.min.js",
            "lib/jquery.mousewheel.js",
            "lib/jquery.mCustomScrollbar.min.js",

            "lib/angular.min.js",
            "lib/angular-sanitize.min.js",
            "lib/angular-route.min.js",
            "lib/angular-ui-router.min.js"
        ],
        "matches": [
            "http://*/*",
            "https://*/*"
        ],
        "run_at": "document_end"
    }],
    "content_security_policy": "script-src 'self'; object-src 'self",
    "description": "This extension demonstrates injection",
    "manifest_version": 2,
    "name": "CheapWatcher",
    "permissions": [
        "http://*/",
        "https://*/",
        "<all_urls>",
        "tabs",
        "cookies"
    ],
    "version": "1.0",
    "web_accessible_resources": [
        "views/*.html",
        "src/*.js",
        "css/*.css",
        "lib/*",
        "img/*.png",
        "img/*.gif",
        "img/*.ico",
        "fonts/*.ttf",
        "fonts/*.otf"
    ]
}

我正在从background.js

调用chrome.tabs.executeScript
 chrome.browserAction.onClicked.addListener(function (tab) {
    if (pageRefreshed === true || pageRefreshed === false && sidebarActive === false && buttonClickedCount === 0 && newTab === false || newTab === true) {
        chrome.tabs.executeScript(null, {file: "src/injection.js"});
        chrome.tabs.executeScript(null, {file: "src/app.js"});
        chrome.tabs.executeScript(null, {file: "src/MainController.js"});
        chrome.tabs.executeScript(null, {file: "src/LoginController.js"});
        chrome.tabs.executeScript(null, {file: "src/ListController.js"});
        chrome.tabs.executeScript(null, {file: "src/ErrorController.js"});
        chrome.tabs.executeScript(null, {file: "src/NotifyController.js"});
        chrome.tabs.executeScript(null, {file: "src/TargetController.js"});
        chrome.tabs.executeScript(null, {file: "src/RegisterController.js"});
        chrome.tabs.executeScript(null, {file: "src/ResetPassController.js"});
        chrome.tabs.executeScript(null, {file: "src/ScrollTargetController.js"});
        chrome.tabs.executeScript(null, {file: "src/SetTargetController.js"});
        chrome.tabs.executeScript(tab.id, {
            code: "$('.cheap-watcher' ).animate({ 'right': '0px' }, 'slow');"
        });
        sidebarActive = true;
        buttonClickedCount = 1;
        newTab = false;
        pageRefreshed = false;
    }
    else if (sidebarActive === true && buttonClickedCount > 0) {
        chrome.tabs.executeScript(tab.id, {
            code: "$('.cheap-watcher' ).animate({ 'right': '-370px' }, 'slow');"
        });
        sidebarActive = false;
    }
    else if (sidebarActive === false && buttonClickedCount > 0) {
        chrome.tabs.executeScript(tab.id, {
            code: "$('.cheap-watcher' ).animate({ 'right': '0px' }, 'slow');"
        });
        sidebarActive = true;
    }
});

是的,我知道这种编码是一种不好的做法,但我只是想让我的扩展工作,但到目前为止还没有运气。消息传递也无济于事。

1 个答案:

答案 0 :(得分:0)

所以我所做的一切都很好,但我唯一遗漏的是手动引导我的角度模块,所以我只是在我的SetTargetController.js中编写了这段代码(因为它是最后一个注入的角度文件)

angular.element(document).ready(function() {
  angular.bootstrap(document, ['app']);
});