本文介绍了Chrome扩展程序:webRequest重定向到现有标签,无需打开新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户打开某个页面时,我想在打开新标签之前在现有标签中打开该页面。我试过用webRequest:

  chrome.webRequest.onBeforeRequest.addListener(
function(details){
chrome.tabs.query({url:'https://example.com/'},函数(标签){
chrome.tabs.update(tabs [0] .id,{url:details。 url,highlight:true});
})
},
{url:['https://example.com/']},
['blocking']
);

我也尝试过使用webNavigation:

  chrome.webNavigation.onBeforeNavigate.addListener(function(details){
if(details.url ==https://example.com/){
chrome.tabs.query({url:'https://example.com/'},函数(标签){
chrome.tabs.update(tabs [0] .id,{url:details.url ,突出显示:true});
});
}
});

这两个选项都会打开一个必须关闭的新选项卡,并且会导致浏览器跳转之间的标签。



有没有什么办法可以在新标签打开之前拦截请求?

谢谢! / p>

解决方案

我最终将其整理出来。这是一个近似的代码:

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

if(matchMysiteUrl){
var pattern ='*://'+ matchMysiteUrl [1] +'.mysite.com / *';
$ b $ 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(
函数(详情){
openMypageInMysite({id:details.tabId,url: details.url});
},
{url:[{hostSuffix:'mysite.com'}]}
);


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!

解决方案

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'}]}
);

这篇关于Chrome扩展程序:webRequest重定向到现有标签,无需打开新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 23:12