In this extension, if I click on the extension button and if current URL matches http://example.com/*
or https://example.com/*
it should be redirected to a new domain like http://NewDomain/*
or https://NewDomain/*
.
在此扩展程序中,如果我点击扩展程序按钮,如果当前网址与http://example.com/*或https://example.com/*匹配,则应将其重定向到新网域,例如http:// NewDomain / *或https:// NewDomain / *。
But I want this extension scan tab URL and perform redirection automatically in the following conditions:
但我想要这个扩展扫描选项卡URL并在以下条件下自动执行重定向:
-
if a new tab created or
如果创建了新标签或
-
if current tab URL changes
如果当前标签页更改
This is my code:
这是我的代码:
manifest.json
的manifest.json
{
"manifest_version": 2,
"name": "My Extension",
"description": "Redirect example.com to NewDomain",
"version": "0.1",
"permissions":
[
"activeTab",
"tabs",
"background"
],
"browser_action":
{
"default_icon": "icon.png"
},
"content_scripts":
[
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content.js"]
}
]
}
scripts.js
scripts.js中
var current_url = tab.url.split('/');
if(current_url[2] == "example.com")
{
current_url[2] = "NewDomain";
}
var new_url = current_url.join('/');
console.log('New tab url: ' + new_url);
document.location.href = new_url;
But it's not working, What changes should I apply?
但它不起作用,我应该采取哪些改变?
1 个解决方案
#1
0
Use a content script instead and drop the browser_action.
请改用内容脚本并删除browser_action。
Then you can use document.location.href = "urlToRedirectTo";
然后你可以使用document.location.href =“urlToRedirectTo”;
The url matching can be done in the manifest
网址匹配可以在清单中完成
You have two options. Either specify the url to match in the manifest or match every url and check the url in the content script.
你有两个选择。指定要在清单中匹配的url或匹配每个url并检查内容脚本中的url。
For example, this manifest will match all urls:
例如,此清单将匹配所有网址:
{
"manifest_version": 2,
"name": "whatever",
"description": "blah",
"version": "1.0",
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content.js"]
}
],
"permissions": [
"activeTab"
]
}
And in the content.js, you could do something like this:
在content.js中,您可以执行以下操作:
if (document.location.href == "theUrlImLookingFor")
document.location.href = "theUrlThatIRedirectTo";
#1
0
Use a content script instead and drop the browser_action.
请改用内容脚本并删除browser_action。
Then you can use document.location.href = "urlToRedirectTo";
然后你可以使用document.location.href =“urlToRedirectTo”;
The url matching can be done in the manifest
网址匹配可以在清单中完成
You have two options. Either specify the url to match in the manifest or match every url and check the url in the content script.
你有两个选择。指定要在清单中匹配的url或匹配每个url并检查内容脚本中的url。
For example, this manifest will match all urls:
例如,此清单将匹配所有网址:
{
"manifest_version": 2,
"name": "whatever",
"description": "blah",
"version": "1.0",
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content.js"]
}
],
"permissions": [
"activeTab"
]
}
And in the content.js, you could do something like this:
在content.js中,您可以执行以下操作:
if (document.location.href == "theUrlImLookingFor")
document.location.href = "theUrlThatIRedirectTo";