1

Good evening everyone ,

I am beginning a chrome extension and in a certain scenario I need to redirect (change URL) of a user's tab .

Here's my code

function changeTabURL(tabName,addr) {
var tabId=parseInt(localStorage.getItem(tabName)); //fetch tab ID

chrome.tabs.update(tabId,{"url":addr});

}

Now here's what's happening , The Chrome:// ... thing is being prepended to my URL ! Say I try to redirect the tab to 'http://www.google.com' , this is what happens :

"No webpage was found for the web address: chrome-extension://oihdngeahhchnacpilhnmaknneooabbc/http://www.google.com"

I can't shake this ! I've tried resetting the URL first

chrome.tabs.get(tabId,function(tab) {
tab.url='';
alert(tab.url);
});
chrome.tabs.update(tabId,{"url":addr});
}

nothing I do shakes this .

Any thoughts?

Joel Blum
  • 7,750
  • 10
  • 41
  • 60
  • I tried the code that you have mentioned here in a content script. I'm not having any problems with the redirection. I tried `www.google.com` at first which gave me an error like the one that you have mentioned here. But then I edited the URL to `http://www.google.com` and tried again after reloading the extension from chrome settings. This worked fine for me. – Jophin Joseph May 09 '12 at 04:43
  • Hey Joseph , thanks ! I have no idea how and why but it's as you said , when I make sure the addresses have http:// the problem is solves ... – Joel Blum May 09 '12 at 08:50

2 Answers2

3

Since you are already using the chrome.tabs API, you may want to try using chrome.tabs.query to find the active tab and get it's id that way. Here's an example:

queryInfo = new Object();
queryInfo.active = true;
chrome.tabs.query(queryInfo, function(result) {
     var activeTab = result[1].id;
     updateProperties = new Object();
     updateProperties.url = 'YOUR_URL_HERE';
     chrome.tabs.update(activeTab, updateProperties, function() {
          // Anything else you want to do after the tab has been updated.
     });
});
Jordan M.
  • 310
  • 2
  • 13
0

have you set the permission in your manifest.json like this:

"permissions": [
"notifications",
"contextMenus",
"tabs",
"contentSettings",
"http://*/*",
"https://*/*"

]

Ericpoon
  • 267
  • 1
  • 2
  • 11