perf: 优化 chrome 插件

pull/10966/head
ibuler 2023-07-13 20:01:06 +08:00
parent bedc83bd3a
commit d95e7c2e24
2 changed files with 56 additions and 40 deletions

View File

@ -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);
}
})

View File

@ -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');
// 遍历 <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) {
});