Chrome Extension: webRequest Redirect to Existing Tab, Without Opening New Tab

ognockocaten

When a user opens a certain page, I want to open that page in an existing tab, before a new tab is opened. I've tried with webRequest:

chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        chrome.tabs.query({ url: 'https://example.com/' },function (tabs) {
            chrome.tabs.update(tabs[0].id, { url: details.url, highlighted: true });
        })
    },
    {urls: ['https://example.com/']},
    ['blocking']
);

I've also tried with webNavigation:

chrome.webNavigation.onBeforeNavigate.addListener(function (details) {
    if(details.url=="https://example.com/") {
        chrome.tabs.query({ url: 'https://example.com/' },function (tabs) {
            chrome.tabs.update(tabs[0].id, { url: details.url, highlighted: true });
        });
    }
});

Both of these open a new tab that has to be closed, and it causes the browser to jump around between tabs.

Is there any way to intercept requests before a new tab is opened?

Thanks!

ognockocaten

I eventually sorted it out. Here's an approximation of the code:

function openMypageInMysite(tab) {
  var urlNavigatingTo = tab.url,
    matchMysiteUrl = urlNavigatingTo.match(/^https?:\/\/(.*).mysite.com\/?(.*)$/);

  if (matchMysiteUrl) {
    var pattern  = '*://' + matchMysiteUrl[1] + '.mysite.com/*';

    chrome.tabs.query({ url: pattern }, function(openTabs) {
      var route    = '/' + matchMysiteUrl[2];
      Mytab = (openTabs[0].id !== tab.id) ? openTabs[0].id : openTabs[1].id;

      chrome.tabs.sendMessage(Mytab, { route: route });
      chrome.tabs.update(Mytab, { active: true });
      chrome.tabs.remove(tab.id);
    });
  }
}

chrome.webNavigation.onBeforeNavigate.addListener(
  function (details) {
    openMypageInMysite({id: details.tabId, url: details.url});
  },
  {url: [{hostSuffix: 'mysite.com'}]}
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Window not opening in a new tab

From Dev

Chrome extension to redirect tab to my page

From Dev

Python - Selenium : Submit form without opening new tab or new window

From Dev

Chrome Extension: webRequest Redirect to Existing Tab, Without Opening New Tab

From Dev

Chrome Extension: executeScript on tab

From Dev

Trigger Chrome extension on new tab open

From Dev

Chrome Extension: Open new tab without losing popup focus

From Dev

Chrome extension open new tab on new tab

From Dev

Is Chrome extension Content Script scope shared when opening a new tab with window.open (since Chrome 45)?

From Dev

laravel redirect to a new tab

From Dev

Opening chrome extension in new tab

From Dev

Google chrome extension to open the New Tab page

From Dev

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

From Dev

Google Chrome Extension default in a new tab

From Dev

Chrome extension run js in new tab

From Dev

Opening a new browser tab

From Dev

How to open a new tab in Chrome in an existing/running Chrome with cmd

From Dev

Restrict addeventlistener of chrome webrequest to the current tab or the main window of the extension

From Dev

Google Chrome shortcut for opening link on new tab?

From Dev

Opening popup in new tab

From Dev

Chrome Extension: Open new tab without losing popup focus

From Dev

Chrome no-longer pre-selects existing text in address bar when opening new tab

From Dev

Simple Chrome Extension : Open an url in a new tab

From Dev

Opening new tab in the background

From Dev

Google Chrome Extension default in a new tab

From Dev

Firefox/Chrome disable search opening in a new tab

From Dev

Form targetting an iframe is opening new tab in Chrome

From Dev

chrome extension - update tab without changing url

From Dev

How do I get Chrome to un-minimized when activating an existing or opening a new tab?

Related Related

  1. 1

    Window not opening in a new tab

  2. 2

    Chrome extension to redirect tab to my page

  3. 3

    Python - Selenium : Submit form without opening new tab or new window

  4. 4

    Chrome Extension: webRequest Redirect to Existing Tab, Without Opening New Tab

  5. 5

    Chrome Extension: executeScript on tab

  6. 6

    Trigger Chrome extension on new tab open

  7. 7

    Chrome Extension: Open new tab without losing popup focus

  8. 8

    Chrome extension open new tab on new tab

  9. 9

    Is Chrome extension Content Script scope shared when opening a new tab with window.open (since Chrome 45)?

  10. 10

    laravel redirect to a new tab

  11. 11

    Opening chrome extension in new tab

  12. 12

    Google chrome extension to open the New Tab page

  13. 13

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

  14. 14

    Google Chrome Extension default in a new tab

  15. 15

    Chrome extension run js in new tab

  16. 16

    Opening a new browser tab

  17. 17

    How to open a new tab in Chrome in an existing/running Chrome with cmd

  18. 18

    Restrict addeventlistener of chrome webrequest to the current tab or the main window of the extension

  19. 19

    Google Chrome shortcut for opening link on new tab?

  20. 20

    Opening popup in new tab

  21. 21

    Chrome Extension: Open new tab without losing popup focus

  22. 22

    Chrome no-longer pre-selects existing text in address bar when opening new tab

  23. 23

    Simple Chrome Extension : Open an url in a new tab

  24. 24

    Opening new tab in the background

  25. 25

    Google Chrome Extension default in a new tab

  26. 26

    Firefox/Chrome disable search opening in a new tab

  27. 27

    Form targetting an iframe is opening new tab in Chrome

  28. 28

    chrome extension - update tab without changing url

  29. 29

    How do I get Chrome to un-minimized when activating an existing or opening a new tab?

HotTag

Archive