|
| 1 | +let cachedStorage; |
| 2 | +let cachedStyles; |
| 3 | + |
1 | 4 | chrome.runtime.onInstalled.addListener(async function (object) { |
| 5 | + cachedStorage = (await chrome.storage.sync.get("features"))?.features || ""; |
| 6 | + cachedStyles = await getEnabledStyles(); |
2 | 7 | try { |
3 | 8 | var featureData = await (await fetch("/features/features.json")).json(); |
4 | 9 | } catch (err) { |
@@ -79,6 +84,15 @@ chrome.runtime.onInstalled.addListener(async function (object) { |
79 | 84 | } |
80 | 85 | }); |
81 | 86 |
|
| 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 | + |
82 | 96 | chrome.tabs.onUpdated.addListener(async function (tabId, info) { |
83 | 97 | var tab = await chrome.tabs.get(tabId); |
84 | 98 | if (tab?.url?.startsWith("https://scratch.mit.edu/")) { |
@@ -450,29 +464,6 @@ chrome.tabs.onUpdated.addListener(async function (tabId, info) { |
450 | 464 | world: newData.world?.toUpperCase() || "MAIN", |
451 | 465 | }); |
452 | 466 | } |
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 | | - } |
476 | 467 | }); |
477 | 468 | } else { |
478 | 469 | chrome.scripting.executeScript({ |
@@ -520,19 +511,42 @@ chrome.runtime.onMessageExternal.addListener(async function ( |
520 | 511 | storagePromises.forEach(function (promise) { |
521 | 512 | if (promise.key === key && !promise.resolved) { |
522 | 513 | promise.resolve(value); |
523 | | - promise.resolved = true |
| 514 | + promise.resolved = true; |
524 | 515 | } |
525 | 516 | }); |
526 | 517 | } |
527 | 518 | } |
528 | 519 | } |
529 | 520 | }); |
530 | 521 |
|
| 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 | + |
531 | 542 | chrome.runtime.onMessage.addListener(async function ( |
532 | 543 | msg, |
533 | 544 | sender, |
534 | 545 | sendResponse |
535 | 546 | ) { |
| 547 | + if (msg.action === "getStyles") { |
| 548 | + sendResponse({ data: cachedStyles }); |
| 549 | + } |
536 | 550 | if (msg?.text === "get-logged-in-user") { |
537 | 551 | sendResponse(true); |
538 | 552 | const data = await ( |
|
0 commit comments