diff --git a/js/moz-parser.js b/js/moz-parser.js index f47ef844..bf023fdc 100644 --- a/js/moz-parser.js +++ b/js/moz-parser.js @@ -144,7 +144,7 @@ var mozParser = (() => { return { // Parse mozilla-format userstyle into sections parse(text) { - return loadScript('/vendor-overwrites/csslint/csslint-worker.js') + return Promise.resolve(self.CSSLint || loadScript('/vendor-overwrites/csslint/csslint-worker.js')) .then(() => parseMozFormat(text)); }, format(style) { diff --git a/js/usercss.js b/js/usercss.js index 3bcf0c1a..b3a8a07d 100644 --- a/js/usercss.js +++ b/js/usercss.js @@ -102,6 +102,8 @@ var usercss = (() => { const RX_STRING_BACKTICK = /(`(?:\\`|[\s\S])*?`)\s*/y; const RX_STRING_QUOTED = /((['"])(?:\\\2|[^\n])*?\2|\w+)\s*/y; + const worker = {}; + function getMetaSource(source) { const commentRe = /\/\*[\s\S]*?\*\//g; const metaRe = /==userstyle==[\s\S]*?==\/userstyle==/i; @@ -474,25 +476,11 @@ var usercss = (() => { const sVars = simpleVars(vars); - return Promise.resolve().then(() => { - // preprocess - if (builder.preprocess) { - return builder.preprocess(sourceCode, sVars); - } - return sourceCode; - }).then(mozStyle => - // moz-parser - loadScript('/js/moz-parser.js').then(() => - mozParser.parse(mozStyle).then(sections => { - style.sections = sections; - }) - ) - ).then(() => { - // postprocess - if (builder.postprocess) { - return builder.postprocess(style.sections, sVars); - } - }).then(() => style); + return Promise.resolve(builder.preprocess && builder.preprocess(sourceCode, sVars) || sourceCode) + .then(mozStyle => invokeWorker({action: 'parse', code: mozStyle})) + .then(sections => (style.sections = sections)) + .then(() => builder.postprocess && builder.postprocess(style.sections, sVars)) + .then(() => style); } function simpleVars(vars) { @@ -579,5 +567,24 @@ var usercss = (() => { } } + function invokeWorker(message) { + if (!worker.queue) { + worker.instance = new Worker('/vendor-overwrites/csslint/csslint-worker.js'); + worker.queue = []; + worker.instance.onmessage = ({data}) => { + worker.queue.shift().resolve(data); + if (worker.queue.length) { + worker.instance.postMessage(worker.queue[0].message); + } + }; + } + return new Promise(resolve => { + worker.queue.push({message, resolve}); + if (worker.queue.length === 1) { + worker.instance.postMessage(message); + } + }); + } + return {buildMeta, buildCode, assignVars}; })(); diff --git a/vendor-overwrites/csslint/csslint-worker.js b/vendor-overwrites/csslint/csslint-worker.js index c4ba86cd..eeb44d98 100644 --- a/vendor-overwrites/csslint/csslint-worker.js +++ b/vendor-overwrites/csslint/csslint-worker.js @@ -10967,5 +10967,12 @@ self.onmessage = ({data: {action = 'run', code, config}}) => { return m; })); return; + + case 'parse': + if (!self.mozParser) { + self.importScripts('/js/moz-parser.js'); + } + mozParser.parse(code) + .then(sections => self.postMessage(sections)); } };