Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google chrome extension Chrome扩展:创建一个新选项卡,等待它完成加载,执行脚本_Google Chrome Extension - Fatal编程技术网

Google chrome extension Chrome扩展:创建一个新选项卡,等待它完成加载,执行脚本

Google chrome extension Chrome扩展:创建一个新选项卡,等待它完成加载,执行脚本,google-chrome-extension,Google Chrome Extension,我已经能够加载一个新选项卡,但脚本在页面加载之前执行 manifest.json: { "manifest_version": 2, "name": "cpClips", "version": "1.0", "description": "cpClips extension to download videos from streaming sites.", "browser_action": { "default_icon": "icon.png" }, "

我已经能够加载一个新选项卡,但脚本在页面加载之前执行

manifest.json:

{
  "manifest_version": 2,
  "name": "cpClips",
  "version": "1.0",
  "description": "cpClips extension to download videos from streaming sites.",
  "browser_action": {
    "default_icon": "icon.png"
  },
  "background": {
    "scripts": ["bg.js"],
    "persistent": false
  },
  "permissions": [
    "tabs"
  ]
}
chrome.browserAction.onClicked.addListener(function(activeTab)
{
    chrome.tabs.create({ url: 'http://127.0.0.1:8000' },function(tab) {
        alert('hi');

   });

});
bg.js:

{
  "manifest_version": 2,
  "name": "cpClips",
  "version": "1.0",
  "description": "cpClips extension to download videos from streaming sites.",
  "browser_action": {
    "default_icon": "icon.png"
  },
  "background": {
    "scripts": ["bg.js"],
    "persistent": false
  },
  "permissions": [
    "tabs"
  ]
}
chrome.browserAction.onClicked.addListener(function(activeTab)
{
    chrome.tabs.create({ url: 'http://127.0.0.1:8000' },function(tab) {
        alert('hi');

   });

});
我尝试使用
chrome.tabs.executeScript
chrome.tabs.onUpdated.addListener
,但即使是新选项卡也无法打开


举个例子就好了。

在创建选项卡时会回调
create
,但不一定在加载页面时回调。您还需要向事件添加一个侦听器,并查看传递给其回调的
状态
changeInfo
对象

chrome.browserAction.onClicked.addListener(function(activeTab)
{
    chrome.tabs.create({url:'http://127.0.0.1:8000'}, function(tab) {
    });
});

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    // make sure the status is 'complete' and it's the right tab
    if (tab.url.indexOf('127.0.0.1:8000') != -1 && changeInfo.status == 'complete') {
        chrome.tabs.executeScript(null, { 
            code: "alert('hi');" 
        });
    }
});