Monday, May 13, 2024
48
rated 0 times [  54] [ 6]  / answers: 1 / hits: 62152  / 15 Years ago, sat, december 12, 2009, 12:00:00

I want to create an extension that redirects the user to another website if he clicks on the extension button. So far I have only seen extensions which create a new tab for each click.



Is it possible to redirect the user to another website using the active tab?



I tried something like this:



chrome.browserAction.onClicked.addListener(function(tab) {
var url = https://www.mipanga.com/Content/Submit?url=
+ encodeURIComponent(tab.url)
+ &title= + encodeURIComponent(tab.title);

document.location.href = url; // <-- this does not work
});

More From » google-chrome

 Answers
41

Attention: If you develop cross-browser extensions (I hope you do!), I recommend that you use chrome.tabs.query(). Please see Jean-Marc Amon's answer for more information. This answer still works in both Firefox and Chrome, but query() is more commonly used, has more options, and works in background pages and popup views.



From the chrome.tabs API, you can use getCurrent(), query(), or update().


Right now, I prefer update() as this allows you to update the current tab without needing to do much else.

NB: You cannot use update() from content scripts.


If updating the url from a content script is required then you should look to use query instead. Jean-Marc Amon's answer provides a wonderful example of how to get the active tab in this case (don't forget to upvote him!).


update()


let myNewUrl = `https://www.mipanga.com/Content/Submit?url=${encodeURIComponent(tab.url)}&title=${encodeURIComponent(tab.title)}`;
chrome.tabs.update(undefined, { url: myNewUrl });

Here, we have set the first argument of update to undefined. This is the tab id that you're wanting to update. If it's undefined then Chrome will update the current tab in the current window.


Please see Domino's answer for more information on update and also note that undefined is not needed. Again, please don't forget to upvote their answer as wellif you find it useful.


getCurrent()


getCurrent also cannot be called from a non-tab context (eg a background page or popup view).


Once you have the current tab, simply pass update().


chrome.tabs.getCurrent(function (tab) {
//Your code below...
let myNewUrl = `https://www.mipanga.com/Content/Submit?url=${encodeURIComponent(tab.url)}&title=${encodeURIComponent(tab.title)}`;

//Update the url here.
chrome.tabs.update(tab.id, { url: myNewUrl });
});

NB: In order to use this this functionality, you must ensure that you have the tabs permission enabled in your manifest.json file:


"permissions": [
"tabs"
],

[#98086] Wednesday, December 9, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alekgermanb

Total Points: 616
Total Questions: 86
Total Answers: 105

Location: Aland Islands
Member since Thu, Dec 23, 2021
2 Years ago
alekgermanb questions
Sun, Mar 20, 22, 00:00, 2 Years ago
Sat, Jan 2, 21, 00:00, 3 Years ago
Sun, Nov 17, 19, 00:00, 5 Years ago
;