Redirect to url Using Google chrome Extension

Jot Dhaliwal

I am new to Google Chrome Extensions. I have created a button in my extension. With that button I want to redirect the user to another site (like "www.example.com"). I have the following code which I wrote for the redirection, but it doesn't work.

Related question.

manifest.json

{
    "name": "Popup ",
    "manifest_version": 2,
    "version": "0.1",
    "description": "Run process on page activated by click in extension popup",
    "browser_action": {
    "default_icon": "icon.png",
        "default_popup": "popup.html"
    },

    "permissions": [
        "tabs", "http://*/*", "https://*/*"
    ]
}

popup.html

<html>
    <head>
        <script src="popup.js"></script>
        <style type="text/css" media="screen">
            body { min-width:250px; text-align: center; }
            #click-me { font-size: 15px; }
        </style>
    </head>
    <body>
        <button id='click-me'>Click Me!</button>
    </body>
</html>

background.js

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

popup.js

function clickHandler(e) {

    chrome.extension.sendRequest({redirect: "https://www.google.co.in"});
    alert("url");

    this.close();
}

document.addEventListener('DOMContentLoaded', function () {
    document.getElementById('click-me').addEventListener('click', clickHandler);
})

Do you have any idea why it doesn't work?

Rob W

If you use background pages, then you need to declare the background script (background.js in your case) in the manifest file:

  "background": {
      "scripts": [ "background.js" ]
  },

Your example will not work though, because sender.tab is only defined if the request came from a tab or content script, not from the popup.

In your example, there is no need for a background page at all, you can just use the chrome.tabs API directly from the popup page:

function clickHandler(e) {
    chrome.tabs.update({url: "https://example.com"});
    window.close(); // Note: window.close(), not this.close()
}
document.addEventListener('DOMContentLoaded', function() {
    document.getElementById('click-me').addEventListener('click', clickHandler);
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Google Chrome Extension Opening Url

From Dev

Google Chrome Extension Redirect different page

From Dev

How to redirect some pages in Google chrome extension?

From Dev

Using Google Charts in Chrome extension

From Dev

OnBeforeRequest URL redirect in Firefox Addon (Conversion from Chrome Extension)

From Dev

How to provide redirect URL for OAuth2 in Chrome extension?

From Dev

Redirect to a chrome-extension URL from a content script?

From Dev

How to redirect current tab's url in Chrome extension?

From Dev

Access Local Files using a Google Chrome Extension

From Dev

Defining a hotkey in Google documents using Chrome extension

From Dev

Using a PAC file in a Chrome Extension to redirect HTTPS requests seems to fail

From Dev

Redirect Twitter Home to Twitter Notifications using Chrome Extension?

From Dev

Redirect from a Chrome Extension Popup

From Dev

Authenticate Chrome extension with non-Google API using Google account

From Dev

Chrome extension navigate to URL

From Dev

Google Chrome inactivity redirect

From Dev

I'm using Chrome stable on OSX how to redirect a url

From Dev

angularjs and google chrome extension

From Dev

angularjs and google chrome extension

From Dev

Error with google chrome extension

From Dev

Google Chrome extension malware?

From Dev

How To Build an API URL using Postman Chrome extension

From Dev

Injecting jQuery into Google.com using a Chrome Extension

From Dev

Changing the background of the google search page using my chrome extension

From Dev

Using google closure library inside chrome extension content script

From Dev

Is there a way to pass information from Google Chrome using an extension?

From Dev

Chrome Extension using DevTools

From Dev

Secure Google Redirect URL

From Dev

Google result redirect url

Related Related

  1. 1

    Google Chrome Extension Opening Url

  2. 2

    Google Chrome Extension Redirect different page

  3. 3

    How to redirect some pages in Google chrome extension?

  4. 4

    Using Google Charts in Chrome extension

  5. 5

    OnBeforeRequest URL redirect in Firefox Addon (Conversion from Chrome Extension)

  6. 6

    How to provide redirect URL for OAuth2 in Chrome extension?

  7. 7

    Redirect to a chrome-extension URL from a content script?

  8. 8

    How to redirect current tab's url in Chrome extension?

  9. 9

    Access Local Files using a Google Chrome Extension

  10. 10

    Defining a hotkey in Google documents using Chrome extension

  11. 11

    Using a PAC file in a Chrome Extension to redirect HTTPS requests seems to fail

  12. 12

    Redirect Twitter Home to Twitter Notifications using Chrome Extension?

  13. 13

    Redirect from a Chrome Extension Popup

  14. 14

    Authenticate Chrome extension with non-Google API using Google account

  15. 15

    Chrome extension navigate to URL

  16. 16

    Google Chrome inactivity redirect

  17. 17

    I'm using Chrome stable on OSX how to redirect a url

  18. 18

    angularjs and google chrome extension

  19. 19

    angularjs and google chrome extension

  20. 20

    Error with google chrome extension

  21. 21

    Google Chrome extension malware?

  22. 22

    How To Build an API URL using Postman Chrome extension

  23. 23

    Injecting jQuery into Google.com using a Chrome Extension

  24. 24

    Changing the background of the google search page using my chrome extension

  25. 25

    Using google closure library inside chrome extension content script

  26. 26

    Is there a way to pass information from Google Chrome using an extension?

  27. 27

    Chrome Extension using DevTools

  28. 28

    Secure Google Redirect URL

  29. 29

    Google result redirect url

HotTag

Archive