Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,30 @@ let injectedCSS = {};
* @param {string} text
*/
async function insertOrReplaceCss(tabId, category, text) {
if (tabId in injectedCSS && category in injectedCSS[tabId]) {
if (injectedCSS[tabId][category] == text) {
return;
}
const previousCss = injectedCSS[tabId]?.[category];
if (previousCss == text) {
return;
}

// Insert new CSS...
await chrome.scripting.insertCSS({
target: { tabId },
css: text
});

if (previousCss) {
// ...and remove old CSS *afterwards* to prevent FOUT
await chrome.scripting.removeCSS({
target: { tabId },
css: injectedCSS[tabId][category]
css: previousCss
});
}

if (!(tabId in injectedCSS)) {
injectedCSS[tabId] = {};
}

injectedCSS[tabId][category] = text;
await chrome.scripting.insertCSS({
target: { tabId },
css: text
});
}

// Do the thing! (Generate stylesheet and inject into active tab)
Expand Down