Skip to content

Commit 6aab8a2

Browse files
committed
Fix styles not being injected when service worker not running
1 parent 0a07441 commit 6aab8a2

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

extras/background.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,39 @@ chrome.tabs.onUpdated.addListener(async function (tabId, info) {
347347
ScratchTools.Features.data = dataFeatures;
348348
}
349349
addData();
350+
cachedStyles = await getEnabledStyles();
351+
var theStyles = [];
352+
cachedStyles.forEach(function (el) {
353+
el.url = chrome.runtime.getURL(
354+
`/features/${style.feature.id}/${style.file}`
355+
);
356+
theStyles.push(el.url);
357+
});
358+
await chrome.scripting.executeScript({
359+
args: [theStyles],
360+
target: { tabId: tabId },
361+
func: injectStyles,
362+
world: "MAIN",
363+
});
364+
ScratchTools.console.log("Injected styles.");
365+
function injectStyles(styles) {
366+
if (!document.querySelector(".scratchtools-styles-div *")) {
367+
var div = document.createElement("div");
368+
div.className = "scratchtools-styles-div";
369+
document.head.appendChild(div);
370+
styles.forEach(function (style) {
371+
if (new URL(tab.url).pathname.match(style.runOn)) {
372+
var link = document.createElement("link");
373+
link.rel = "stylesheet";
374+
link.href = style.url;
375+
link.dataset.feature = style.feature.id;
376+
document
377+
.querySelector(".scratchtools-styles-div")
378+
.appendChild(link);
379+
}
380+
});
381+
}
382+
}
350383
for (var i in data) {
351384
var feature = data[i];
352385
if (feature.version === 2) {
@@ -531,7 +564,7 @@ async function getEnabledStyles() {
531564
).styles;
532565
if (styles) {
533566
for (var i2 in styles) {
534-
styles[i2].feature = feature
567+
styles[i2].feature = feature;
535568
allStyles.push(styles[i2]);
536569
}
537570
}

0 commit comments

Comments
 (0)