如何使用Google Chrome扩展程序&内容脚本?
问题描述:
我目前正在构建Google Chrome浏览器扩展程序,该扩展程序会测试某些模式,如果找到,它们会将其重定向到新的网址。
I'm currently building a Google Chrome extension which tests for certain patterns and if found, redirects them to a new URL.
我已经获取了模式检查通过内容脚本完成,现在我不知道如何才能继续完成重定向。任何建议?
I've gotten the pattern checking done via a content script, and now I'm not sure how can I proceed with getting the redirect done. Any suggestions ?
答
将内容脚本中的重定向网址发送到后台页面:
Send redirect url from a content script to a background page:
chrome.runtime.sendMessage({redirect: "http://redirect"});
在会导致重定向的后台页面更新标签页中:
In a background page update tab's url which would cause redirect:
chrome.runtime.onMessage.addListener(function(request, sender) {
chrome.tabs.update(sender.tab.id, {url: request.redirect});
});