Skip to content

Commit

Permalink
fix: reactivation of the URL Changed event (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienfontaine authored Jun 22, 2024
1 parent ed330f1 commit 6d6ac15
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 78 deletions.
40 changes: 20 additions & 20 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<chrome.tabs.Tab[]> {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -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[],
Expand Down
62 changes: 4 additions & 58 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 6d6ac15

Please sign in to comment.