diff --git a/edit.js b/edit.js index fe3644fd..638f9d4f 100644 --- a/edit.js +++ b/edit.js @@ -12,6 +12,9 @@ var useHistoryBack; // use browser history back when "back to manage" is click var propertyToCss = {urls: "url", urlPrefixes: "url-prefix", domains: "domain", regexps: "regexp"}; var CssToProperty = {"url": "urls", "url-prefix": "urlPrefixes", "domain": "domains", "regexp": "regexps"}; +// if background page hasn't been loaded yet, increase the chances it has before DOMContentLoaded +onBackgroundReady(); + // make querySelectorAll enumeration code readable ["forEach", "some", "indexOf", "map"].forEach(function(method) { NodeList.prototype[method]= Array.prototype[method]; @@ -246,9 +249,8 @@ function initCodeMirror() { return options.map(function(opt) { return ""; }).join(""); } var themeControl = document.getElementById("editor.theme"); - var bg = chrome.extension.getBackgroundPage(); - if (bg && bg.codeMirrorThemes) { - themeControl.innerHTML = optionsHtmlFromArray(bg.codeMirrorThemes); + if (BG && BG.codeMirrorThemes) { + themeControl.innerHTML = optionsHtmlFromArray(BG.codeMirrorThemes); } else { // Chrome is starting up and shows our edit.html, but the background page isn't loaded yet themeControl.innerHTML = optionsHtmlFromArray([theme == "default" ? t("defaultTheme") : theme]); @@ -1822,7 +1824,15 @@ function onRuntimeMessage(request) { switch (request.method) { case "styleUpdated": if (styleId && styleId == request.style.id && request.reason != 'editSave') { - initWithStyle(request); + if ((request.style.sections[0] || {}).code === null) { + // the code-less style came from notifyAllTabs + onBackgroundReady().then(() => { + request.style = BG.cachedStyles.byId.get(request.style.id); + initWithStyle(request); + }); + } else { + initWithStyle(request); + } } break; case "styleDeleted": diff --git a/messaging.js b/messaging.js index 44705306..91be8798 100644 --- a/messaging.js +++ b/messaging.js @@ -32,19 +32,22 @@ if (!BG || BG != window) { function notifyAllTabs(msg) { const originalMessage = msg; - if (msg.codeIsUpdated === false && msg.style) { + if (msg.method == 'styleUpdated' || msg.method == 'styleAdded') { + // apply/popup/manage use only meta for these two methods, + // editor may need the full code but can fetch it directly, + // so we send just the meta to avoid spamming lots of tabs with huge styles msg = Object.assign({}, msg, { style: getStyleWithNoCode(msg.style) }); } const affectsAll = !msg.affects || msg.affects.all; - const affectsOwnOrigin = !affectsAll && (msg.affects.editor || msg.affects.manager); - const affectsTabs = affectsAll || affectsOwnOrigin; + const affectsOwnOriginOnly = !affectsAll && (msg.affects.editor || msg.affects.manager); + const affectsTabs = affectsAll || affectsOwnOriginOnly; const affectsIcon = affectsAll || msg.affects.icon; const affectsPopup = affectsAll || msg.affects.popup; if (affectsTabs || affectsIcon) { // list all tabs including chrome-extension:// which can be ours - chrome.tabs.query(affectsOwnOrigin ? {url: URLS.ownOrigin + '*'} : {}, tabs => { + chrome.tabs.query(affectsOwnOriginOnly ? {url: URLS.ownOrigin + '*'} : {}, tabs => { for (const tab of tabs) { if (affectsTabs || URLS.optionsUI.includes(tab.url)) { chrome.tabs.sendMessage(tab.id, msg);