javascript - In chrome extension content-scripts, how can I redirect the current window to a different url and still have the script continue -
i trying write extension goes through google voice inbox , gets phone numbers have ever contacted me. these numbers , them output them www.editpad.org.
the issue if try redirect www.google.com/voice www.editpad.org, using
window.location.href = "www.editpad.org"
i have tried using background scripts like:
//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(); });
however, once redirects, seems script stops running (console.log doesn't work). believe it's because editpad.org not in manifest.json matching websites.
if add matching websites, script redirect editpad.org, re-run , refresh on , over.
how can redirect page editpad.org , still continue script left off say, "once you're on new page, start executing following function"
my manifest:
{ "manifest_version": 2, "name": "whistle", "description": "this extension searches phone numbers found on google voice account", "version": "1.0", "background": { "scripts": ["background.js"], "persistence": false }, "content_scripts": [ { "matches": ["*://www.google.com/voice/*"], "js": ["jquery.js", "execute.js"] } ] }
background.js
chrome.extension.onrequest.addlistener(function(request, sender, sendresponse) { chrome.tabs.update(sender.tab.id, {url: request.redirect}); sendresponse(); });
in manifest file:
"persistent":true
which default value. need send message containing of numbers background page. when new page loads can send message new content script containing of numbers.
//content script var msg = []; // array containing 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 separate script or 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'){ // whatever the phone numbers } }); }
Comments
Post a Comment