我可以在目标/特定的浏览器标签中打开链接吗?

时间:2019-02-06 06:38:07

标签: javascript google-chrome google-chrome-extension hyperlink tabs

我正在尝试创建Chrome扩展程序。这将迫使链接在特定的目标标签中打开。例如tab[0]tab[1]或两者都适用于不同类别的链接。

在下面共享扩展的代码,该扩展在选项卡中打开一个弹出窗口。 在某些情况下。但我不太了解。

// background script

// get settings
chrome.storage.sync.get({
// default value
t1pop: true,
t1foc: true
}, function(items) {
t1pop = items.t1pop;
t1foc = items.t1foc;

// open pop-up as a tab
chrome.windows.getCurrent({},function(w){
var mainwindow = w.id;
chrome.windows.onCreated.addListener(function(w){
    if(w.type == "popup" && t1pop == true){
        chrome.windows.get(w.id,{populate:true},function(w){

            chrome.tabs.query({
                active: true,
                windowId: w.id
            }, function (tabs) {
                var t1popUrl = tabs[0].url;
                if (t1popUrl.startsWith('chrome-extension://') == false){
                    chrome.tabs.move(w.tabs[0].id,{windowId:mainwindow,index:-1},function(){
                        chrome.tabs.update(w.tabs[0].id,{active:t1foc /* focus new window or not */});
                    });
                }

            });


        });
    }
});

chrome.windows.onFocusChanged.addListener(function(w){
    chrome.windows.get(w,{},function(w){        
        if(w.type == "normal"){
            mainwindow = w.id;
        }
    });
    });
});


});

我已经仔细搜索了问题的堆栈答案,但没有得到答案。 谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我通过将预打开的选项卡的ID存储在数组中来解决此问题。 然后在onBeforeRequest事件中更新这些标签的网址

let slots = [false, false, false, false] //will populate with placeholder tabs.
let preventTabCreation = false //will be true once initial tabs are created..
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
    let url = details.url
    let tabNo;
    let tab_id = details.tabId
    if(!slots.includes(tab_id) && preventTabCreation) {
        for(let i = 1; i < slots.length; i++) {
            if(urlinarr(linkTabs[i-1], url)) {
                tabNo = i;
            }
        }
        chrome.tabs.remove(tab_id)
        chrome.tabs.update(slots[tabNo], {url: url, active: true})
        return {cancel: true}
    }
},
{urls: ["<all_urls>"]},
["blocking"]);

首次使用此代码创建占位符标签。

chrome.browserAction.onClicked.addListener(function() {
  launch()
})

function launch() {
  //using the first tab for google sheets. where the links to be opened are.
  if (!slots[0]) {
    chrome.tabs.create({
      url: "https://docs.google.com/spreadsheets",
      index: 0
    }, function(data) {
      slots[0] = data.id
      startPreventingTabCreation()
    })
  }
  for (let i = 1; i < slots.length; i++) {
    if (!slots[i]) {
      chrome.tabs.create({
        url: chrome.extension.getURL('dummy.html'),
        index: i
      }, function(data) {
        slots[i] = data.id
        startPreventingTabCreation()
      })
    }
  }
}

function startPreventingTabCreation() {
  if (slots.every(x => x != false)) {
    preventTabCreation = true
    chrome.tabs.update(slots[0], {
        active: true
      })
  }
}