Google chrome extension Chrome扩展:创建一个新选项卡,等待它完成加载,执行脚本
我已经能够加载一个新选项卡,但脚本在页面加载之前执行 manifest.json: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_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');"
});
}
});