在 chrome 扩展程序内容脚本中,如何将当前窗口重定向到其他 url 并且仍然让脚本继续
In chrome extension content-scripts, how can I redirect the current window to a different url and still have the script continue
我正在尝试编写一个扩展程序,该扩展程序通过我的Google语音收件箱并获取曾经与我联系过的所有电话号码。我想得到这些数字,然后将它们输出到 www.editpad.org。
问题是如果我尝试从 www.google.com/voice 重定向到 www.editpad.org,我正在使用
window.location.href = "www.editpad.org"
我还尝试使用后台脚本,例如:
//content script
chrome.extension.sendRequest({redirect: "http://www.editpad.org"});
//background
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
chrome.tabs.update(sender.tab.id, {url: request.redirect});
sendREsponse(); });
但是,一旦重定向,脚本似乎停止运行(控制台.log甚至不起作用)。我相信这是因为 editpad.org 不在我的清单.json中用于匹配网站。
如果我将其添加到我的匹配网站,则脚本将重定向到 editpad.org,但随后它将重新运行并一遍又一遍地刷新到自身。
如何将页面重定向到此 editpad.org,并且仍然从中断的地方继续脚本,以便我可以说,"一旦您进入此新页面,就开始执行以下功能"
我的清单:
{
"manifest_version": 2,
"name": "Whistle",
"description": "This extension searches for all phone numbers found on a Google Voice account",
"version": "1.0",
"background": {
"scripts": ["background.js"],
"persistence": false
},
"content_scripts": [
{
"matches": ["*://www.google.com/voice/*"],
"js": ["jQuery.js", "execute.js"]
}
]
}
背景.js
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
chrome.tabs.update(sender.tab.id, {url: request.redirect});
sendREsponse();
});
在清单文件中:
"persistent":true
这是默认值。 您需要将包含所有数字的消息发送回后台页面。 然后,当新页面加载时,您可以向包含所有数字的新内容脚本发送一条消息。
//content script
var msg = []; // array containing your numbers
chrome.runtime.sendMessage({name:"get_numbers", message: msg});
//background
var data = {};
chrome.runtime.onMessage.addListener(function(message){
if (message.name=== "get_numbers"){
data["numbers"] = message.message;
}
else if(message.name === "request_numbers"){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {name: "delegate_message",numbers: data["numbers"});
});
}
});
//content script (either a separate script or the same one)
if (window.location.href === "www.editpad.org"){
chrome.runtime.sendMessage({name:"request_numbers"});
chrome.runtime.onMessage.addListener(function(request){
if(request.name === 'delegate_message'){
// do whatever with the the phone numbers
}
});
}
相关文章:
- java脚本在Chrome和其他浏览器中对LocaleTimeString的不同行为
- 网页上失败的javascript会导致所有其他脚本失败
- 在BoilerplateJS浏览器加载所有脚本(在其他模块中),而不考虑激活的模块
- 如果其他是咖啡脚本
- 我的时间倒计时脚本在谷歌浏览器中工作正常,但在其他浏览器中打开时它显示为南楠
- Selenium异步脚本在自己的线程中阻塞其他脚本
- 编写脚本以关闭其他选项卡或浏览器
- Firefox扩展开发:如何在内容脚本中创建一个全局函数,以便其他加载的脚本文件可以访问它
- 如何测试jQuery和某些其他脚本是否已加载
- 不要为机器人(谷歌和其他)加载JS脚本以获得更好的性能
- 如何在访问其他url时在nodewebkit中注入脚本
- JS脚本由1个元素调用,而不是由其他3个元素调用
- 长轮询脚本会停止所有其他脚本
- 我用脚本创建了一个按钮.我可以通过点击按钮调用其他函数吗?我这样做,但失败了
- 在ASP中调用HTML以调用其他脚本
- 如果其他脚本'装载顺序未知
- 有没有一个简单的脚本可以向美国/加拿大游客显示脚本1,向其他游客显示脚本2
- Pjax.页面上的其他JS脚本不会;不起作用
- jquery破坏了其他脚本
- Meteor脚本确保在其他一切之前执行(在客户端上)