我需要将一些特定的URL重定向到另一个特定的URL。例如,我想重定向";https://www.youtube.com/watch?v=ABCDefgHIJK"至";https://zymernation.com/wp-content/uploads/2019/05/Internet-Access-Error-e1558967567821.jpg">
为此,我制作了一个脚本,可以获取当前URL,但不能重定向URL。
我有一些代码,但它们并不完美。
需要专家指导。
谢谢。
试试这个解决方案:
manifest.json
{
"manifest_version": 2,
"name": "test",
"description": "google chrome extension",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png"
},
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs"
]
}
background.js
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { // listener for tab opens
if (changeInfo.status == 'loading') { // when the page is loading (you can do info.status === 'complete' but you will see the page for a second or two)
if (tab.url === "https://www.youtube.com/watch?v=ABCDefgHIJK") {
chrome.tabs.query({ // change the tab url
currentWindow: true,
active: true
}, function (tab) {
chrome.tabs.update(tab.id, {
url: 'https://zymernation.com/wp-content/uploads/2019/05/Internet-Access-Error-e1558967567821.jpg'
});
});
}
}
})