Scott
Scott

Reputation: 13

Google Chrome Extension - Redirect

The function I'm looking for:

Say I'm browsing "domainname.com", I click the extension, That then redirects me to myurl.com/site/domainname.com. I have searched everywhere and had no luck.

So from what ever "url" the user is browsing, they click the extension and it will redirect them to myurl.com/site/"Url"

Links to a helpful article or some code to finish off my script would greatly appreciated. I have a basic .json file setup from some demo extensions, I need help with grabbing the users current url and then redirect to my site if they click. Thanks in advance...

Edited the question slightly. Hope it make a little more sense now.

Upvotes: 0

Views: 402

Answers (2)

Jimmy Sawczuk
Jimmy Sawczuk

Reputation: 13614

Start by asking for the tabs permission in manifest.json:

...
"permissions": [
    "tabs"
]
...

Next, in a script that runs off of a background page, add this:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
{
    if (tab.status == "complete")
    {
        tab_url = tab.url;

        // ...

tab_url will now contain the name of the current tab whenever that tab is updated. From within that code, you can do whatever you want, in your case, sanitizing the URL and redirecting to http://www.myurl.com/whatever.com using top.location.href.

Upvotes: 2

Joshua Carmody
Joshua Carmody

Reputation: 13730

Your question is hard to understand... but if you're asking what I think you're asking your redirects will probably work properly if you add "http://" to the beginning. If you don't do this, Chrome will assume the link is relative and will use the current domain and path when resolving the link.

Upvotes: 0

Related Questions