How to redirect current tab's url in Chrome extension?
I'm writing a Chrome extension, used to switch between different search engines. The manifest.json looks like:
"browser_action":
"default_popup": "popup.html"
,
"permissions": [
"tabs",
"activeTab"
],
"content_security_policy": "script-src 'self'; object-src 'self'",
"content_scripts": [
"matches": ["*://*.baidu.com/*"],
"js": ["content.js"]
]
content.jsis used to get user's keywords in searching.popup.htmlcontains a list of supported search engines.
When user clicks on search engine's icon in popup window, I need to redirect current tab to a new url using the search engine user selected.
Now I can get keyword. Problem is: How to redirect current tab to new url?
javascript
add a comment |
I'm writing a Chrome extension, used to switch between different search engines. The manifest.json looks like:
"browser_action":
"default_popup": "popup.html"
,
"permissions": [
"tabs",
"activeTab"
],
"content_security_policy": "script-src 'self'; object-src 'self'",
"content_scripts": [
"matches": ["*://*.baidu.com/*"],
"js": ["content.js"]
]
content.jsis used to get user's keywords in searching.popup.htmlcontains a list of supported search engines.
When user clicks on search engine's icon in popup window, I need to redirect current tab to a new url using the search engine user selected.
Now I can get keyword. Problem is: How to redirect current tab to new url?
javascript
Which part presents a problem? Have you already done the keyword detection and click handlers in the popup?
– Xan
May 10 '16 at 9:37
Yes, I can get keyword now. But I don't know how to redirect to new url. Do you know how to do this?
– zhm
May 10 '16 at 9:43
add a comment |
I'm writing a Chrome extension, used to switch between different search engines. The manifest.json looks like:
"browser_action":
"default_popup": "popup.html"
,
"permissions": [
"tabs",
"activeTab"
],
"content_security_policy": "script-src 'self'; object-src 'self'",
"content_scripts": [
"matches": ["*://*.baidu.com/*"],
"js": ["content.js"]
]
content.jsis used to get user's keywords in searching.popup.htmlcontains a list of supported search engines.
When user clicks on search engine's icon in popup window, I need to redirect current tab to a new url using the search engine user selected.
Now I can get keyword. Problem is: How to redirect current tab to new url?
javascript
I'm writing a Chrome extension, used to switch between different search engines. The manifest.json looks like:
"browser_action":
"default_popup": "popup.html"
,
"permissions": [
"tabs",
"activeTab"
],
"content_security_policy": "script-src 'self'; object-src 'self'",
"content_scripts": [
"matches": ["*://*.baidu.com/*"],
"js": ["content.js"]
]
content.jsis used to get user's keywords in searching.popup.htmlcontains a list of supported search engines.
When user clicks on search engine's icon in popup window, I need to redirect current tab to a new url using the search engine user selected.
Now I can get keyword. Problem is: How to redirect current tab to new url?
javascript
javascript
edited May 10 '16 at 9:44
zhm
asked May 10 '16 at 9:35
zhmzhm
2,17631728
2,17631728
Which part presents a problem? Have you already done the keyword detection and click handlers in the popup?
– Xan
May 10 '16 at 9:37
Yes, I can get keyword now. But I don't know how to redirect to new url. Do you know how to do this?
– zhm
May 10 '16 at 9:43
add a comment |
Which part presents a problem? Have you already done the keyword detection and click handlers in the popup?
– Xan
May 10 '16 at 9:37
Yes, I can get keyword now. But I don't know how to redirect to new url. Do you know how to do this?
– zhm
May 10 '16 at 9:43
Which part presents a problem? Have you already done the keyword detection and click handlers in the popup?
– Xan
May 10 '16 at 9:37
Which part presents a problem? Have you already done the keyword detection and click handlers in the popup?
– Xan
May 10 '16 at 9:37
Yes, I can get keyword now. But I don't know how to redirect to new url. Do you know how to do this?
– zhm
May 10 '16 at 9:43
Yes, I can get keyword now. But I don't know how to redirect to new url. Do you know how to do this?
– zhm
May 10 '16 at 9:43
add a comment |
2 Answers
2
active
oldest
votes
There are 2 possible approaches:
A. Let the popup handle redirection.
B. Let the content script handle redirection.
In both cases, you need to solve 2 problems:
Communicate information between the two. In approach A, you need to get the keywords from the content script. In approach B, you need to tell the content script which engine to switch to.
Both are solved using Messaging. I recommend messaging from the popup to the content script using
chrome.tabs.sendMessage(and responding in approach A), because in the other direction content script doesn't know when to send the message (popup may be closed).Actually trigger the change. In approach A,
chrome.tabs.updatedoes the trick. In approach B, content script can changewindow.locationto navigate away.
That works great! :-D Though I have a confusion. Whychrome.tabs.updateonly redirect current tab, not all tabs?
– zhm
May 10 '16 at 9:57
It usually takes a first argument,tabId, but when it's not provided it defaults to currently active tab.
– Xan
May 10 '16 at 9:59
Actually, same withtabs.sendMessage: it defaults to active tab.
– Xan
May 10 '16 at 10:01
That explains a lot. Thanks!
– zhm
May 10 '16 at 10:09
add a comment |
See chrome.tabs.update, you could update the url of current tab via the following code
chrome.tabs.update(url: "http://www.baidu.com");
Considering you get the keyword from content script, and want to redirect the url after user clicks some button in popup page, you may need Message Passing or chrome.storage to share the keywords between content script and popup page.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37134567%2fhow-to-redirect-current-tabs-url-in-chrome-extension%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are 2 possible approaches:
A. Let the popup handle redirection.
B. Let the content script handle redirection.
In both cases, you need to solve 2 problems:
Communicate information between the two. In approach A, you need to get the keywords from the content script. In approach B, you need to tell the content script which engine to switch to.
Both are solved using Messaging. I recommend messaging from the popup to the content script using
chrome.tabs.sendMessage(and responding in approach A), because in the other direction content script doesn't know when to send the message (popup may be closed).Actually trigger the change. In approach A,
chrome.tabs.updatedoes the trick. In approach B, content script can changewindow.locationto navigate away.
That works great! :-D Though I have a confusion. Whychrome.tabs.updateonly redirect current tab, not all tabs?
– zhm
May 10 '16 at 9:57
It usually takes a first argument,tabId, but when it's not provided it defaults to currently active tab.
– Xan
May 10 '16 at 9:59
Actually, same withtabs.sendMessage: it defaults to active tab.
– Xan
May 10 '16 at 10:01
That explains a lot. Thanks!
– zhm
May 10 '16 at 10:09
add a comment |
There are 2 possible approaches:
A. Let the popup handle redirection.
B. Let the content script handle redirection.
In both cases, you need to solve 2 problems:
Communicate information between the two. In approach A, you need to get the keywords from the content script. In approach B, you need to tell the content script which engine to switch to.
Both are solved using Messaging. I recommend messaging from the popup to the content script using
chrome.tabs.sendMessage(and responding in approach A), because in the other direction content script doesn't know when to send the message (popup may be closed).Actually trigger the change. In approach A,
chrome.tabs.updatedoes the trick. In approach B, content script can changewindow.locationto navigate away.
That works great! :-D Though I have a confusion. Whychrome.tabs.updateonly redirect current tab, not all tabs?
– zhm
May 10 '16 at 9:57
It usually takes a first argument,tabId, but when it's not provided it defaults to currently active tab.
– Xan
May 10 '16 at 9:59
Actually, same withtabs.sendMessage: it defaults to active tab.
– Xan
May 10 '16 at 10:01
That explains a lot. Thanks!
– zhm
May 10 '16 at 10:09
add a comment |
There are 2 possible approaches:
A. Let the popup handle redirection.
B. Let the content script handle redirection.
In both cases, you need to solve 2 problems:
Communicate information between the two. In approach A, you need to get the keywords from the content script. In approach B, you need to tell the content script which engine to switch to.
Both are solved using Messaging. I recommend messaging from the popup to the content script using
chrome.tabs.sendMessage(and responding in approach A), because in the other direction content script doesn't know when to send the message (popup may be closed).Actually trigger the change. In approach A,
chrome.tabs.updatedoes the trick. In approach B, content script can changewindow.locationto navigate away.
There are 2 possible approaches:
A. Let the popup handle redirection.
B. Let the content script handle redirection.
In both cases, you need to solve 2 problems:
Communicate information between the two. In approach A, you need to get the keywords from the content script. In approach B, you need to tell the content script which engine to switch to.
Both are solved using Messaging. I recommend messaging from the popup to the content script using
chrome.tabs.sendMessage(and responding in approach A), because in the other direction content script doesn't know when to send the message (popup may be closed).Actually trigger the change. In approach A,
chrome.tabs.updatedoes the trick. In approach B, content script can changewindow.locationto navigate away.
answered May 10 '16 at 9:50
XanXan
53.7k10104131
53.7k10104131
That works great! :-D Though I have a confusion. Whychrome.tabs.updateonly redirect current tab, not all tabs?
– zhm
May 10 '16 at 9:57
It usually takes a first argument,tabId, but when it's not provided it defaults to currently active tab.
– Xan
May 10 '16 at 9:59
Actually, same withtabs.sendMessage: it defaults to active tab.
– Xan
May 10 '16 at 10:01
That explains a lot. Thanks!
– zhm
May 10 '16 at 10:09
add a comment |
That works great! :-D Though I have a confusion. Whychrome.tabs.updateonly redirect current tab, not all tabs?
– zhm
May 10 '16 at 9:57
It usually takes a first argument,tabId, but when it's not provided it defaults to currently active tab.
– Xan
May 10 '16 at 9:59
Actually, same withtabs.sendMessage: it defaults to active tab.
– Xan
May 10 '16 at 10:01
That explains a lot. Thanks!
– zhm
May 10 '16 at 10:09
That works great! :-D Though I have a confusion. Why
chrome.tabs.update only redirect current tab, not all tabs?– zhm
May 10 '16 at 9:57
That works great! :-D Though I have a confusion. Why
chrome.tabs.update only redirect current tab, not all tabs?– zhm
May 10 '16 at 9:57
It usually takes a first argument,
tabId, but when it's not provided it defaults to currently active tab.– Xan
May 10 '16 at 9:59
It usually takes a first argument,
tabId, but when it's not provided it defaults to currently active tab.– Xan
May 10 '16 at 9:59
Actually, same with
tabs.sendMessage: it defaults to active tab.– Xan
May 10 '16 at 10:01
Actually, same with
tabs.sendMessage: it defaults to active tab.– Xan
May 10 '16 at 10:01
That explains a lot. Thanks!
– zhm
May 10 '16 at 10:09
That explains a lot. Thanks!
– zhm
May 10 '16 at 10:09
add a comment |
See chrome.tabs.update, you could update the url of current tab via the following code
chrome.tabs.update(url: "http://www.baidu.com");
Considering you get the keyword from content script, and want to redirect the url after user clicks some button in popup page, you may need Message Passing or chrome.storage to share the keywords between content script and popup page.
add a comment |
See chrome.tabs.update, you could update the url of current tab via the following code
chrome.tabs.update(url: "http://www.baidu.com");
Considering you get the keyword from content script, and want to redirect the url after user clicks some button in popup page, you may need Message Passing or chrome.storage to share the keywords between content script and popup page.
add a comment |
See chrome.tabs.update, you could update the url of current tab via the following code
chrome.tabs.update(url: "http://www.baidu.com");
Considering you get the keyword from content script, and want to redirect the url after user clicks some button in popup page, you may need Message Passing or chrome.storage to share the keywords between content script and popup page.
See chrome.tabs.update, you could update the url of current tab via the following code
chrome.tabs.update(url: "http://www.baidu.com");
Considering you get the keyword from content script, and want to redirect the url after user clicks some button in popup page, you may need Message Passing or chrome.storage to share the keywords between content script and popup page.
edited Nov 12 '18 at 17:09
Sasikanth
2,62011242
2,62011242
answered May 10 '16 at 9:48
Haibara AiHaibara Ai
7,77521539
7,77521539
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37134567%2fhow-to-redirect-current-tabs-url-in-chrome-extension%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Which part presents a problem? Have you already done the keyword detection and click handlers in the popup?
– Xan
May 10 '16 at 9:37
Yes, I can get keyword now. But I don't know how to redirect to new url. Do you know how to do this?
– zhm
May 10 '16 at 9:43