Chris
Chris

Reputation: 57

In chrome extension content-scripts, how can I redirect the current window to a different url and still have the script continue

I am trying to write an extension that goes through my google voice inbox and gets all phone numbers that have ever contacted me. I would like to get these numbers and them output them to www.editpad.org.

The issue is if I try to redirect from www.google.com/voice to www.editpad.org, I am using

window.location.href = "www.editpad.org"

I have also 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 it redirects, it seems like the script stops running (console.log doesn't even work). I believe it's because editpad.org is not in my manifest.json for matching websites.

If I add it to my matching websites, then the script will redirect to editpad.org, but then it will re-run and refresh to itself over and over.

How can I redirect the page to this editpad.org and still continue the script from where it left off so that I could say, "once you're on this new page, start executing the following function"

My manifest:

{


"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"]
    }
  ]
}

Background.js

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    chrome.tabs.update(sender.tab.id, {url: request.redirect});
    sendREsponse();
});

Upvotes: 1

Views: 2342

Answers (1)

nick
nick

Reputation: 1207

In your manifest file:

"persistent":true

which is the default value. You need to send a message containing all of the numbers back to the background page. Then when the new page loads you can send a message to the new content script containing all of the numbers.

//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
         }
     });
}

Upvotes: 1

Related Questions