From d95e7c2e24c034b36e04861ec36ee2f3247d753c Mon Sep 17 00:00:00 2001 From: ibuler Date: Thu, 13 Jul 2023 20:01:06 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20chrome=20=E6=8F=92?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../disable_new_tab_window_menu/background.js | 63 ++++++++++++++----- .../content_script.js | 33 +++------- 2 files changed, 56 insertions(+), 40 deletions(-) diff --git a/apps/terminal/applets/chrome/extensions/disable_new_tab_window_menu/background.js b/apps/terminal/applets/chrome/extensions/disable_new_tab_window_menu/background.js index 57b0cca1a..7f2556ab8 100644 --- a/apps/terminal/applets/chrome/extensions/disable_new_tab_window_menu/background.js +++ b/apps/terminal/applets/chrome/extensions/disable_new_tab_window_menu/background.js @@ -1,23 +1,54 @@ // background.js +const tabs = [] +const debug = console.log + // 监听标签页的创建事件 chrome.tabs.onCreated.addListener(function (tab) { // 获取当前窗口的所有标签页 - chrome.tabs.query({currentWindow: true}, function (tabs) { - // 如果当前窗口的标签页数量大于1,则关闭新创建的标签页 - if (tabs.length > 1) { - chrome.tabs.remove(tab.id); - } - }); + debug('New tab add, tabs : ', tabs) + tabs.push(tab) }); -// 监听窗口的创建事件 -chrome.windows.onCreated.addListener(function (window) { -// 获取当前所有窗口 - chrome.windows.getAll(function (windows) { - // 如果当前窗口数量大于1,则关闭新创建的窗口 - if (windows.length > 1) { - chrome.windows.remove(window.id); - } - }); -}); +chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { + debug('Tab changed xx: ', tabId, changeInfo, tab) + if (changeInfo.status !== 'loading') { + return + } + const tabFind = tabs.findIndex(t => t.id === tabId) + if (tabFind === -1) { + debug('Tab not found: ', tabId, tabs) + return + } + Object.assign(tabs[tabFind], tab) + + const blockUrls = ['chrome://newtab/'] + if (!tab.url || blockUrls.includes(tab.url) || tab.url.startsWith('chrome://')) { + debug('Blocked url, destroy: ', tab.url) + chrome.tabs.remove(tabId); + return + } + + // 第一个 tab 不做限制 + // 修改初始 tab 的状态,因为第一个 tab 没有地址栏,可以允许它自由跳转 + if (tabs.length === 1) { + debug('First tab, pass') + return + } + + const firstUrl = tabs[0].url + const curUrl = tab.url + if (!firstUrl.startsWith('http') || !curUrl.startsWith('http')) { + debug('First tab url empty, or current empty, pass ', firstUrl, curUrl) + return + } + + const firstTabHost = new URL(firstUrl).host + const curHost = new URL(curUrl).host + const firstDomain = firstTabHost.split('.').slice(-2).join('.') + const curDomain = curHost.split('.').slice(-2).join('.') + if (firstDomain !== curDomain) { + debug('Not same domain, destroy: ', firstTabHost, ' current: ', curHost) + chrome.tabs.remove(tabId); + } +}) diff --git a/apps/terminal/applets/chrome/extensions/disable_new_tab_window_menu/content_script.js b/apps/terminal/applets/chrome/extensions/disable_new_tab_window_menu/content_script.js index 0b6162430..3b2ba828c 100644 --- a/apps/terminal/applets/chrome/extensions/disable_new_tab_window_menu/content_script.js +++ b/apps/terminal/applets/chrome/extensions/disable_new_tab_window_menu/content_script.js @@ -1,5 +1,7 @@ // content_script.js +const debug = console.log + // 创建一个 Mutation Observer 实例 const observer = new MutationObserver(function (mutationsList) { // 遍历每个发生变化的 mutation @@ -10,7 +12,7 @@ const observer = new MutationObserver(function (mutationsList) { const links = document.getElementsByTagName('a'); // 遍历 标签元素并修改链接属性 - console.log("开始替换标签") + debug("开始替换标签") for (let i = 0; i < links.length; i++) { links[i].target = '_self'; // 将 target 属性设置为 _self,当前窗口打开 } @@ -27,43 +29,26 @@ const observer = new MutationObserver(function (mutationsList) { // 开始观察 document.body 的子节点变化 observer.observe(document.body, {childList: true, subtree: true}); -chrome.runtime.onMessage.addListener( - function (request, sender, sendResponse) { - console.log(request.url); - $("iframe").attr("src", request.url); - sendResponse({farewell: "goodbye"}); - } -) - document.addEventListener("contextmenu", function (event) { - console.log('On context') + debug('On context') event.preventDefault(); }); -var AllowedKeys = ['P', 'F', 'C', 'V'] +const AllowedKeys = ['P', 'F', 'C', 'V'] window.addEventListener("keydown", function (e) { if (e.key === "F12" || (e.ctrlKey && !AllowedKeys.includes(e.key.toUpperCase()))) { e.preventDefault(); e.stopPropagation(); - console.log('Press key: ', e.ctrlKey ? 'Ctrl' : '', e.shiftKey ? ' Shift' : '', e.key) + debug('Press key: ', e.ctrlKey ? 'Ctrl' : '', e.shiftKey ? ' Shift' : '', e.key) } }, true); -// 保存原始的 window.open 函数引用 -var originalOpen = window.open; - // 修改 window.open 函数 window.open = function (url, target, features) { // 将 target 强制设置为 "_self",使得新页面在当前标签页中打开 target = "_self"; - - // 修改当前页面的 URL - location.href = url; - + debug('Open url: ', url, target, features) // 调用原始的 window.open 函数 - return originalOpen.call(this, url, target, features); + window.href = url + // return originalOpen.call(this, url, target, features); }; - - -chrome.runtime.sendMessage({greeting: "hello"}, function (response) { -});