Skip to content

Commit ea89317

Browse files
authored
Merge pull request #537 from rgantzos/main
2 parents a30477c + 069cba9 commit ea89317

File tree

4 files changed

+93
-31
lines changed

4 files changed

+93
-31
lines changed

api/main.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ ScratchTools.waitForElements = function (selector, callback, id, rework) {
4646
}
4747
};
4848

49-
var stylesDiv = document.createElement("div");
50-
stylesDiv.className = "scratchtools-styles-div";
51-
document.head.appendChild(stylesDiv);
49+
var stylesDiv = document.querySelector("div.scratchtools-styles-div");
5250
ScratchTools.waitForElements("head > *", function (el) {
5351
if (el !== stylesDiv) {
5452
document.head.appendChild(stylesDiv);

extras/background.js

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
let cachedStorage;
2+
let cachedStyles;
3+
14
chrome.runtime.onInstalled.addListener(async function (object) {
5+
cachedStorage = (await chrome.storage.sync.get("features"))?.features || "";
6+
cachedStyles = await getEnabledStyles();
27
try {
38
var featureData = await (await fetch("/features/features.json")).json();
49
} catch (err) {
@@ -79,6 +84,15 @@ chrome.runtime.onInstalled.addListener(async function (object) {
7984
}
8085
});
8186

87+
chrome.storage.onChanged.addListener(async function (changes, namespace) {
88+
for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
89+
if (key === "features") {
90+
cachedStorage = newValue;
91+
cachedStyles = await getEnabledStyles();
92+
}
93+
}
94+
});
95+
8296
chrome.tabs.onUpdated.addListener(async function (tabId, info) {
8397
var tab = await chrome.tabs.get(tabId);
8498
if (tab?.url?.startsWith("https://scratch.mit.edu/")) {
@@ -450,29 +464,6 @@ chrome.tabs.onUpdated.addListener(async function (tabId, info) {
450464
world: newData.world?.toUpperCase() || "MAIN",
451465
});
452466
}
453-
}
454-
});
455-
newData.styles?.forEach(function (style) {
456-
if (new URL(tab.url).pathname.match(style.runOn)) {
457-
chrome.scripting.executeScript({
458-
args: [
459-
data[el].id,
460-
chrome.runtime.getURL(
461-
`/features/${data[el]["id"]}/${style.file}`
462-
),
463-
],
464-
target: { tabId: tabId },
465-
func: insertCSS,
466-
world: "MAIN",
467-
});
468-
function insertCSS(feature, path) {
469-
var link = document.createElement("link");
470-
link.rel = "stylesheet";
471-
link.href = path;
472-
link.dataset.feature = feature;
473-
document.querySelector(".scratchtools-styles-div").appendChild(link);
474-
}
475-
}
476467
});
477468
} else {
478469
chrome.scripting.executeScript({
@@ -520,19 +511,42 @@ chrome.runtime.onMessageExternal.addListener(async function (
520511
storagePromises.forEach(function (promise) {
521512
if (promise.key === key && !promise.resolved) {
522513
promise.resolve(value);
523-
promise.resolved = true
514+
promise.resolved = true;
524515
}
525516
});
526517
}
527518
}
528519
}
529520
});
530521

522+
async function getEnabledStyles() {
523+
var allStyles = [];
524+
var data = (await (await fetch(`/features/features.json`)).json()).filter(
525+
(el) => el.version === 2 && cachedStorage.includes(el.id)
526+
);
527+
for (var i in data) {
528+
var feature = data[i];
529+
var styles = (
530+
await (await fetch(`/features/${feature.id}/data.json`)).json()
531+
).styles;
532+
if (styles) {
533+
for (var i2 in styles) {
534+
styles[i2].feature = feature
535+
allStyles.push(styles[i2]);
536+
}
537+
}
538+
}
539+
return allStyles;
540+
}
541+
531542
chrome.runtime.onMessage.addListener(async function (
532543
msg,
533544
sender,
534545
sendResponse
535546
) {
547+
if (msg.action === "getStyles") {
548+
sendResponse({ data: cachedStyles });
549+
}
536550
if (msg?.text === "get-logged-in-user") {
537551
sendResponse(true);
538552
const data = await (

extras/inject-styles.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
async function getAllUserstyles() {
2+
var div = document.createElement("div");
3+
div.className = "scratchtools-styles-div";
4+
document.head.appendChild(div);
5+
var styles = await getStyles();
6+
styles.forEach(function (style) {
7+
if (window.location.pathname.match(style.runOn)) {
8+
var link = document.createElement("link");
9+
link.rel = "stylesheet";
10+
link.href = chrome.runtime.getURL(`/features/${style.feature.id}/${style.file}`);
11+
link.dataset.feature = style.feature.id;
12+
document.querySelector(".scratchtools-styles-div").appendChild(link);
13+
}
14+
});
15+
}
16+
17+
var injectStylesWaitForHead = new MutationObserver(injectStyles);
18+
injectStylesWaitForHead.observe(document.querySelector("html"), {
19+
childList: true,
20+
});
21+
22+
async function injectStyles() {
23+
if (document.head) {
24+
injectStylesWaitForHead.disconnect();
25+
getAllUserstyles();
26+
}
27+
}
28+
29+
async function getStyles() {
30+
return new Promise((resolve, reject) => {
31+
chrome.runtime.sendMessage({ action: "getStyles" }, function (response) {
32+
resolve(response.data || "");
33+
});
34+
});
35+
}

manifest.json

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,25 @@
2121
"48": "/extras/icons/icon48.png",
2222
"128": "/extras/icons/icon128.png"
2323
},
24-
"web_accessible_resources": [{
25-
"resources": ["features/*", "/extras/feature/index.html", "/api/*", "/extras/icons/*"],
26-
"matches": ["https://scratch.mit.edu/*"]
27-
}],
24+
"content_scripts": [
25+
{
26+
"matches": ["https://scratch.mit.edu/*"],
27+
"run_at": "document_start",
28+
"js": ["extras/inject-styles.js"],
29+
"all_frames": true
30+
}
31+
],
32+
"web_accessible_resources": [
33+
{
34+
"resources": [
35+
"features/*",
36+
"/extras/feature/index.html",
37+
"/api/*",
38+
"/extras/icons/*"
39+
],
40+
"matches": ["https://scratch.mit.edu/*"]
41+
}
42+
],
2843
"host_permissions": ["https://scratch.mit.edu/"],
2944
"browser_specific_settings": {
3045
"gecko": {

0 commit comments

Comments
 (0)