Skip to content

Commit

Permalink
Merge branch 'master' of github.com:sunwu51/notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwu51 committed May 23, 2024
2 parents 69178f0 + c836667 commit 45bd32a
Showing 1 changed file with 5 additions and 21 deletions.
26 changes: 5 additions & 21 deletions 23.09/reuse_tabs/service_worker.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
var reuse = true;
var reuse = false;

// 点击的时候打开Panel
chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true }).catch((error) => console.error(error));

// 页面commit的时候检查是不是已经打开了相同url的tab,如果有则跳转过去
// chrome.webNavigation.onCommitted.addListener(
// async ({tabId, url}) => {
// if (url.length > 0 && reuse) {
// // 如果有已经打开的,相同url的tab
// var tabs = await chrome.tabs.query({url});
// tabs = tabs.filter(it=> it.id != tabId)
// if(tabs.length > 0){
// var oldTab = tabs[0];
// // 跳转到最近打开的相同的tab
// await chrome.tabs.remove([tabId]);
// await chrome.windows.update(oldTab.windowId, {focused: true});
// await chrome.tabs.update(oldTab.id, {active: true});
// }
// }
// }
// )
chrome.tabs.onCreated.addListener((e => console.log("tabsonCreated", e)));

chrome.tabs.onActivated.addListener(function (tabId, selectInfo) {
Expand All @@ -42,7 +25,7 @@ chrome.webNavigation.onCompleted.addListener(async e => {

chrome.webNavigation.onDOMContentLoaded.addListener(async e => {
console.log("onDOMContentLoaded", e)
if (e.url && e.url.length > 0 && e.url !== 'about:blank' && e.tabId && e.frameId === 0) {
if (reuse && e.url && e.url.length > 0 && e.url !== 'about:blank' && e.tabId && e.frameId === 0) {
var url = e.url.split("#")[0];
var tabs = await chrome.tabs.query({ url });
tabs = tabs.filter(it => it.id != e.tabId)
Expand All @@ -56,10 +39,11 @@ chrome.webNavigation.onDOMContentLoaded.addListener(async e => {
}
});

chrome.runtime.onMessage.addListener(async (msg) => {
chrome.runtime.onMessage.addListener(async (msg, sender, sendResponse) => {
if (msg.type === 'setting') {
if (msg.key === 'reuse-check') reuse = msg.value;
}
sendResponse({ received: true });
});


Expand Down Expand Up @@ -88,4 +72,4 @@ function lcs(words, text) {
}
}
return arr[m][n];
}
}

0 comments on commit 45bd32a

Please sign in to comment.