【发布时间】:2015-11-08 19:36:53
【问题描述】:
我正在尝试制作一个 chrome 扩展,在加载时重定向到其他页面。例如,如果它是 google.nl,请转到 google.nl?example 我设法让它工作,但只有当我按下扩展按钮时。我想从 background.js 运行脚本,但出现错误:
未捕获的类型错误:无法读取未定义的属性“onBeforeRequest”
为什么我不想重新加载?好吧,我没有。这只是为了测试。原计划是读取 URL 并将打开的图数据放入我的扩展中。
清单(部分)
"content_scripts": [{
"matches": [
"<all_urls>"
],
"js": ["background.js"]
}],
"permissions": [
"tabs",
"activeTab",
"webRequest"
],
"background": {
"scripts": ["background.js"]
}
background.js
chrome.webRequest.onBeforeRequest.addListener(function(a) {
if (a.url.indexOf("http://www.google.com/") >= 0) {
reloadExtensions();
chrome.tabs.get(a.tabId, function(b) {
if ((b.selected == false)) {
chrome.tabs.remove(a.tabId)
}
});
return {redirectUrl: chrome.extension.getURL("close.html")}
}
return {cancel: false}
}, {urls: ["http://reload.extensions/"],types: ["main_frame"]}, ["blocking"]);
【问题讨论】: