From 60693e8c3f2273c218905f392333e987d002b4c0 Mon Sep 17 00:00:00 2001 From: eight Date: Sat, 1 Jun 2019 17:10:44 +0800 Subject: [PATCH] Fix: change disabled state after the content is set (#701) --- content/apply.js | 10 +++++----- content/style-injector.js | 11 ++++------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/content/apply.js b/content/apply.js index c3dbaec3..f1132746 100644 --- a/content/apply.js +++ b/content/apply.js @@ -89,17 +89,18 @@ const APPLY = (() => { // for getPreventDefault which got removed in FF59 https://bugzil.la/691151 const EVENT_NAME = chrome.runtime.id; let ready; - return (el, content) => + return (el, content, disabled) => checkPageScript().then(ok => { if (!ok) { - const disabled = el.disabled; el.textContent = content; + // https://github.com/openstyles/stylus/issues/693 el.disabled = disabled; } else { const detail = pageObject({ method: 'setStyleContent', id: el.id, - content + content, + disabled }); window.dispatchEvent(new CustomEvent(EVENT_NAME, {detail})); } @@ -119,13 +120,12 @@ const APPLY = (() => { const available = checkStyleApplied(); if (available) { window.addEventListener(EVENT_NAME, function handler(e) { - const {method, id, content} = e.detail; + const {method, id, content, disabled} = e.detail; if (method === 'setStyleContent') { const el = document.getElementById(id); if (!el) { return; } - const disabled = el.disabled; el.textContent = content; el.disabled = disabled; } else if (method === 'orphan') { diff --git a/content/style-injector.js b/content/style-injector.js index ad7c02ba..5024d944 100644 --- a/content/style-injector.js +++ b/content/style-injector.js @@ -68,7 +68,7 @@ function createStyleInjector({compare, setStyleContent, onUpdate}) { return update(style); } style.el = createStyle(style.id); - const pending = setStyleContent(style.el, style.code); + const pending = setStyleContent(style.el, style.code, !enabled); table.set(style.id, style); const nextIndex = list.findIndex(i => compare(i, style) > 0); if (nextIndex < 0) { @@ -78,8 +78,6 @@ function createStyleInjector({compare, setStyleContent, onUpdate}) { document.documentElement.insertBefore(style.el, list[nextIndex].el); list.splice(nextIndex, 0, style); } - // disabled flag is read-only when not attached to a document - style.el.disabled = !enabled; return pending; } @@ -110,7 +108,7 @@ function createStyleInjector({compare, setStyleContent, onUpdate}) { oldEl.parentNode.insertBefore(style.el, oldEl.nextSibling); style.el.disabled = !enabled; } - return setStyleContent(style.el, code) + return setStyleContent(style.el, code, !enabled) .then(() => oldEl && oldEl.remove()); } @@ -153,13 +151,12 @@ function createStyleInjector({compare, setStyleContent, onUpdate}) { function sort() { list.sort(compare); for (const style of list) { - // moving an element resets its 'disabled' state - const disabled = style.el.disabled; // FIXME: do we need this? // const copy = document.importNode(el, true); // el.textContent += ' '; // invalidate CSSOM cache document.documentElement.appendChild(style.el); - style.el.disabled = disabled; + // moving an element resets its 'disabled' state + style.el.disabled = !enabled; } }