diff --git a/src/background.ts b/src/background.ts index 46c3aea0..d9e88e2e 100644 --- a/src/background.ts +++ b/src/background.ts @@ -7,13 +7,26 @@ import { _setStorage, } from './common/storage.ts'; -// chrome.tabs.onUpdated.addListener( -// async (_: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) => { -// if (!changeInfo.url) return; -// -// await applyRuleToTab(tab); -// } -// ); +chrome.tabs.onUpdated.addListener( + async (_: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) => { + if (!changeInfo.url) return; + + await applyRuleToTab(tab); + } +); + +async function applyRuleToTab(tab: chrome.tabs.Tab) { + if (!tab.id) return false; + if (!tab.url) return false; + + const rule = await _getRuleFromUrl(tab.url); + + await ungroupTab(rule, tab); + + if (rule) { + await chrome.tabs.sendMessage(tab.id, { action: 'applyRule', rule: rule }); + } +} function queryTabs(queryInfo = {}): Promise { return new Promise((resolve, reject) => { @@ -223,19 +236,6 @@ async function applyGroupRuleToTab( ); } -// async function applyRuleToTab(tab: chrome.tabs.Tab) { -// if (!tab.id) return false; -// if (!tab.url) return false; -// -// const rule = await _getRuleFromUrl(tab.url); -// -// await ungroupTab(rule, tab); -// -// if (rule) { -// await chrome.tabs.sendMessage(tab.id, { action: 'applyRule', rule: rule }); -// } -// } - let handleTabGroupsMaxRetries = 600; async function handleTabGroups( groups: chrome.tabGroups.TabGroup[], diff --git a/src/content.js b/src/content.js index 4d8d6111..3342839d 100644 --- a/src/content.js +++ b/src/content.js @@ -114,26 +114,8 @@ export async function applyRule(ruleParam) { document.head.appendChild(originalTitleElement); } - let originalTitle = originalTitleElement.getAttribute('content'); + const originalTitle = originalTitleElement.getAttribute('content'); document.title = processTitle(location.href, originalTitle, rule); - - const targetNode = document.documentElement; - const config = { childList: true, subtree: true }; - let lastTitle = document.title; - - const callback = function () { - if (document.title !== lastTitle) { - originalTitleElement.setAttribute('content', document.title); - - originalTitle = originalTitleElement.getAttribute('content'); - document.title = processTitle(location.href, originalTitle, rule); - - lastTitle = document.title; - } - }; - - const observer = new MutationObserver(callback); - observer.observe(targetNode, config); } // Pinning, muting handled through Chrome Runtime messages @@ -145,47 +127,9 @@ export async function applyRule(ruleParam) { await chrome.runtime.sendMessage({ action: 'setMuted' }); } - let iconChangedByMe = false; - // Favicon handling if (rule.tab.icon) { processIcon(rule.tab.icon); - - const iconObserver = new MutationObserver((mutations) => { - if (!iconChangedByMe) { - mutations.forEach((mutation) => { - if (mutation.target.type === 'image/x-icon') { - processIcon(rule.tab.icon); - iconChangedByMe = true; - } else if (mutation.addedNodes.length) { - mutation.addedNodes.forEach((node) => { - if (node.type === 'image/x-icon') { - processIcon(rule.tab.icon); - iconChangedByMe = true; - } - }); - } else if (mutation.removedNodes.length) { - mutation.removedNodes.forEach((node) => { - if (node.type === 'image/x-icon') { - processIcon(rule.tab.icon); - iconChangedByMe = true; - } - }); - } - }); - } else { - iconChangedByMe = false; - } - }); - - iconObserver.observe(document.head, { - attributes: true, - childList: true, - characterData: true, - subtree: true, - attributeOldValue: true, - characterDataOldValue: true, - }); } if (rule.tab.protected) { @@ -228,7 +172,9 @@ chrome.runtime.onMessage.addListener(async function (request) { title: title, }); } else if (request.action === 'applyRule') { - await applyRule(request.rule); + setTimeout(async () => { + await applyRule(request.rule); + }, 200); } else if (request.action === 'ungroupTab') { await chrome.tabs.ungroup(request.tabId); }