fix: 修复 chrome 插件不生效的问题

pull/11306/head
ibuler 1 year ago
parent 16ceb79427
commit 40248077cd

@ -6,24 +6,25 @@ const debug = console.log
// 监听标签页的创建事件 // 监听标签页的创建事件
chrome.tabs.onCreated.addListener(function (tab) { chrome.tabs.onCreated.addListener(function (tab) {
// 获取当前窗口的所有标签页 // 获取当前窗口的所有标签页
debug('New tab add, tabs : ', tabs) debug('New tab add, tabs : ', tabs.map(t => t.id))
tabs.push(tab) tabs.push(tab)
}); });
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
debug('Tab changed xx: ', tabId, changeInfo, tab) debug('Tab status changed: ', tabId, ' => ', changeInfo.status)
if (changeInfo.status !== 'loading') { if (changeInfo.status !== 'loading') {
return return
} }
const tabFind = tabs.findIndex(t => t.id === tabId) const tabFind = tabs.findIndex(t => t.id === tabId)
if (tabFind === -1) { if (tabFind === -1) {
debug('Tab not found: ', tabId, tabs) tabs.push(tab)
return } else {
}
Object.assign(tabs[tabFind], tab) Object.assign(tabs[tabFind], tab)
}
const blockUrls = ['chrome://newtab/'] const blockUrls = ['chrome://newtab/']
if (!tab.url || blockUrls.includes(tab.url) || tab.url.startsWith('chrome://')) { if (!tab.url || blockUrls.includes(tab.url) || tab.url.startsWith('chrome://')) {
alert('安全模式,禁止打开新标签页')
debug('Blocked url, destroy: ', tab.url) debug('Blocked url, destroy: ', tab.url)
chrome.tabs.remove(tabId); chrome.tabs.remove(tabId);
return return
@ -52,3 +53,11 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
chrome.tabs.remove(tabId); chrome.tabs.remove(tabId);
} }
}) })
chrome.tabs.onRemoved.addListener(function (tabId, removeInfo) {
debug('Tab removed: ', tabId)
const tabFind = tabs.findIndex(t => t.id === tabId)
if (tabFind !== -1) {
tabs.splice(tabFind, 1)
}
})

@ -7,7 +7,7 @@
"scripts": [ "scripts": [
"background.js" "background.js"
], ],
"persistent": false "persistent": true
}, },
"content_scripts": [ "content_scripts": [
{ {

Loading…
Cancel
Save