在onUpdated处理程序中调用executeScript(chrome扩展)

时间:2014-02-10 23:28:24

标签: google-chrome google-chrome-extension google-chrome-app

我有这段代码:

    chrome.tabs.onUpdated.addListener(function(id, changes, tab)
    {
     if (changes.status != "complete") return false;
     chrome.tabs.executeScript(id, {code: "alert('Page loaded.');"});
    });

它在调试器中执行,但不起作用。为什么呢?

1 个答案:

答案 0 :(得分:0)

请完全按照此处的定义进行尝试。

<强>的manifest.json

{
    "manifest_version": 2,
    "name": "Execute script app",
    "version": "1.0",
    "background": {
        "persistent":true,
        "page":"background.html"
    },
    "content_scripts": [{
            "matches": ["http://*/*"],
            "js": ["app.js"]
        }
    ],
    "permissions": [
        "tabs",
        "http://*/*"
    ]
}

<强> background.html

<script src="background.js"></script>

<强> background.js

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
    if(changeInfo && changeInfo.status == "complete"){
        chrome.tabs.executeScript(tabId, {code: "alert('Page loaded.');"});
    }
});

<强> app.js

//well this is just empty since ur not doing anything here