How to redirect some pages in Google chrome extension?

Merchuk Hul

I am using chrome.webRequest.onBeforeRequest in my background script to listen to all request a user is making. My extension now is just a manifest and this bg script.

I then check if the url matches a regex:

chrome.webRequest.onBeforeRequest.addListener(
     checkRedirect, {urls: ["<all_urls>"]}, ["blocking"]
);

function checkRedirect(details) {   
    var location = getLocation(details.url);

    if(/some_regex/.test(details.url)) {
        var redirect_url = 'http://some_redirect.com' + location.pathname;
        chrome.tabs.update({url: redirect_url});
    }

    return {cancel: false}; 
}

function getLocation(href) {
    var l = document.createElement('a');
    l.href = href;
    return l;
};

So if a user enters a page or clicks on a link to that page, I'd like to redirect him. The code above works almost fine; the problem is when the page requests e.g. CSS then those CSS end up on the user screen.


So what I'd want is to only filter requests to pages / clicks.. how to do this?

Rob W

Limit event notifications to the top-level frame by setting types to ['main_frame'] (use ['main_frame', 'sub_frame'] if you want to include frames as well).

Although the previous suggestion will fix your problem, I suggest to go a step further and get rid of chrome.tabs.update. Use redirectUrl to replace the previous request with a redirect to the desired URL.

If your URL-pattern matching method is very simple, expressible as a match pattern, incorporate the filter in the "urls" key of the webRequest API filter. This will reduce your extension's impact on your browser's performance.

chrome.webRequest.onBeforeRequest.addListener(function(details) {
    var location = getLocation(details.url); // getLocation defined in question
    var redirect_url = 'http://some_redirect.com' + location.pathname;
    return { redirectUrl: redirect_url };
}, {
    types: ['main_frame', 'sub_frame'],
    urls: ['*://*/*some_pattern*']
}, ['blocking']);

Note: Do not use <all_urls> if you are not prepared to handle non-http(s) requests.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Chrome extension is not loaded in some pages

From Dev

Internationalization of HTML pages for my Google Chrome Extension

From Dev

Redirect to url Using Google chrome Extension

From Dev

Google Chrome Extension Redirect different page

From Dev

Writing Google chrome extension that works on some urls

From Dev

How to redirect to html page in chrome extension

From Dev

How to uninstall this Google Chrome extension?

From Dev

In Google Chrome some web pages, the font changes for no apparent reason

From Dev

Arabic (Foreign) language/font on some Google/Chrome pages

From Dev

How to clean up web pages on Chrome extension uninstall/disable

From Dev

How to clean up web pages on Chrome extension uninstall/disable

From Dev

How to provide redirect URL for OAuth2 in Chrome extension?

From Dev

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

From Dev

Chrome automatic redirect on certain pages

From Dev

How to launch a new window in Google Chrome Extension

From Dev

How to use webrtc insde google chrome extension?

From Dev

How to create installer for a Google Chrome Extension

From Dev

How to disable Google Chrome extension autoupdate

From Dev

How to detect if Google Cast extension is installed in Chrome?

From Dev

how to know communication error in google chrome extension

From Dev

Google Chrome Extension: Omnibox Keyword How To?

From Dev

How to Retrieve a Hidden Extension in Google Chrome

From Dev

Some pages in Chrome always redirect to ransom page at http://system-check-fyeltkhn.in

From Dev

Redirect from a Chrome Extension Popup

From Dev

How to use chrome.alarms for Google Chrome extension

From Dev

Google Chrome inactivity redirect

From Dev

angularjs and google chrome extension

From Dev

angularjs and google chrome extension

From Dev

Error with google chrome extension

Related Related

  1. 1

    Chrome extension is not loaded in some pages

  2. 2

    Internationalization of HTML pages for my Google Chrome Extension

  3. 3

    Redirect to url Using Google chrome Extension

  4. 4

    Google Chrome Extension Redirect different page

  5. 5

    Writing Google chrome extension that works on some urls

  6. 6

    How to redirect to html page in chrome extension

  7. 7

    How to uninstall this Google Chrome extension?

  8. 8

    In Google Chrome some web pages, the font changes for no apparent reason

  9. 9

    Arabic (Foreign) language/font on some Google/Chrome pages

  10. 10

    How to clean up web pages on Chrome extension uninstall/disable

  11. 11

    How to clean up web pages on Chrome extension uninstall/disable

  12. 12

    How to provide redirect URL for OAuth2 in Chrome extension?

  13. 13

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

  14. 14

    Chrome automatic redirect on certain pages

  15. 15

    How to launch a new window in Google Chrome Extension

  16. 16

    How to use webrtc insde google chrome extension?

  17. 17

    How to create installer for a Google Chrome Extension

  18. 18

    How to disable Google Chrome extension autoupdate

  19. 19

    How to detect if Google Cast extension is installed in Chrome?

  20. 20

    how to know communication error in google chrome extension

  21. 21

    Google Chrome Extension: Omnibox Keyword How To?

  22. 22

    How to Retrieve a Hidden Extension in Google Chrome

  23. 23

    Some pages in Chrome always redirect to ransom page at http://system-check-fyeltkhn.in

  24. 24

    Redirect from a Chrome Extension Popup

  25. 25

    How to use chrome.alarms for Google Chrome extension

  26. 26

    Google Chrome inactivity redirect

  27. 27

    angularjs and google chrome extension

  28. 28

    angularjs and google chrome extension

  29. 29

    Error with google chrome extension

HotTag

Archive