From ba64b95575349fdbba2b4592f81c709df1ed0262 Mon Sep 17 00:00:00 2001 From: eight Date: Thu, 11 Oct 2018 00:54:38 +0800 Subject: [PATCH] WIP: kill cachedStyles --- background/background.js | 12 +++++ background/style-manager.js | 3 +- background/style-via-api.js | 2 +- background/update.js | 24 ++++++--- background/usercss-helper.js | 3 +- content/apply.js | 62 +++++----------------- content/install-hook-openusercss.js | 1 + content/install-hook-usercss.js | 1 + content/install-hook-userstyles.js | 2 +- edit/applies-to-line-widget.js | 4 +- edit/beautify.js | 6 +-- edit/codemirror-default.js | 2 +- edit/codemirror-factory.js | 3 +- edit/colorpicker-helper.js | 2 +- edit/edit.js | 80 +++-------------------------- edit/editor-worker.js | 1 + {js => edit}/exclusions.js | 13 ++--- edit/global-search.js | 5 +- edit/linter-config-dialog.js | 3 +- edit/linter-defaults.js | 4 +- edit/linter-engines.js | 2 +- edit/linter-help-dialog.js | 3 +- edit/linter-meta.js | 3 +- edit/linter-report.js | 3 +- edit/linter.js | 5 +- edit/live-preview.js | 3 +- edit/match-highlighter-helper.js | 2 +- edit/regexp-tester.js | 6 +-- edit/reroute-hotkeys.js | 3 +- edit/sections-editor.js | 50 ++++++++++++++++-- edit/show-keymap-help.js | 3 +- edit/source-editor.js | 12 ++--- edit/util.js | 1 + install-usercss/install-usercss.js | 4 +- js/cache.js | 2 + js/dom.js | 26 ++++++++++ js/localization.js | 2 + js/messaging.js | 42 ++------------- js/moz-parser.js | 1 + js/msg.js | 1 + js/prefs.js | 1 - js/storage-util.js | 2 +- 42 files changed, 188 insertions(+), 222 deletions(-) rename {js => edit}/exclusions.js (94%) diff --git a/background/background.js b/background/background.js index 634559f2..050bfdc8 100644 --- a/background/background.js +++ b/background/background.js @@ -475,3 +475,15 @@ function updateAllTabsIcon() { tabs.map(t => updateIcon({tab: t})) ); } + +function openEditor({id}) { + let url = '/edit.html'; + if (id) { + url += `?id=${id}`; + } + if (chrome.windows && prefs.get('openEditInWindow')) { + chrome.windows.create(Object.assign({url}, prefs.get('windowPosition'))); + } else { + openURL({url}); + } +} diff --git a/background/style-manager.js b/background/style-manager.js index 66a176d9..8b167db7 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -1,6 +1,7 @@ /* eslint no-eq-null: 0, eqeqeq: [2, "smart"] */ -/* global createCache db calcStyleDigest normalizeStyleSections db promisify +/* global createCache db calcStyleDigest db tryRegExp getStyleWithNoCode msg */ +/* exported styleManager */ 'use strict'; /* diff --git a/background/style-via-api.js b/background/style-via-api.js index c16ddfd5..72c1138e 100644 --- a/background/style-via-api.js +++ b/background/style-via-api.js @@ -1,4 +1,4 @@ -/* global getStyles API_METHODS styleManager */ +/* global API_METHODS styleManager CHROME prefs */ 'use strict'; API_METHODS.styleViaAPI = !CHROME && (() => { diff --git a/background/update.js b/background/update.js index 99bb126b..0f089cf7 100644 --- a/background/update.js +++ b/background/update.js @@ -1,9 +1,7 @@ -/* -global styleSectionsEqual -global calcStyleDigest cachedStyles getStyleWithNoCode -global usercss semverCompare -global API_METHODS styleManager -*/ +/* global styleSectionsEqual prefs download tryJSONparse ignoreChromeError + calcStyleDigest getStyleWithNoCode debounce chromeLocal + usercss semverCompare + API_METHODS styleManager */ 'use strict'; (() => { @@ -70,7 +68,7 @@ global API_METHODS styleManager function checkStyle({ id, - style = cachedStyles.byId.get(id), + style, port, save = true, ignoreDigest, @@ -89,7 +87,7 @@ global API_METHODS styleManager 'ignoreDigest' option is set on the second manual individual update check on the manage page. */ - return Promise.resolve(style) + return fetchStyle() .then([calcStyleDigest][!ignoreDigest ? 0 : 'skip']) .then([checkIfEdited][!ignoreDigest ? 0 : 'skip']) .then([maybeUpdateUSO, maybeUpdateUsercss][style.usercssData ? 1 : 0]) @@ -97,6 +95,16 @@ global API_METHODS styleManager .then(reportSuccess) .catch(reportFailure); + function fetchStyle() { + if (style) { + return Promise.resolve(); + } + return styleManager.get(id) + .then(_style => { + style = _style; + }); + } + function reportSuccess(saved) { log(STATES.UPDATED + ` #${style.id} ${style.name}`); const info = {updated: true, style: saved}; diff --git a/background/usercss-helper.js b/background/usercss-helper.js index 3f058b0d..d0341d64 100644 --- a/background/usercss-helper.js +++ b/background/usercss-helper.js @@ -1,4 +1,5 @@ -/* global API_METHODS usercss chromeLocal styleManager */ +/* global API_METHODS usercss chromeLocal styleManager FIREFOX deepCopy openURL + download */ 'use strict'; (() => { diff --git a/content/apply.js b/content/apply.js index 9bd701bc..faae6303 100644 --- a/content/apply.js +++ b/content/apply.js @@ -1,5 +1,5 @@ /* eslint no-var: 0 */ -/* global msg */ +/* global msg API prefs */ 'use strict'; (() => { @@ -79,18 +79,18 @@ * @param {Function} callback * @returns {Boolean|undefined} */ - function getStylesFallback(msg) { - if (window !== parent && - location.href !== 'about:blank') { - try { - if (parent.location.origin === location.origin && - parent.location.href !== location.href) { - chrome.runtime.connect({name: 'getStyles:' + JSON.stringify(msg)}); - return true; - } - } catch (e) {} - } - } + // function getStylesFallback(msg) { + // if (window !== parent && + // location.href !== 'about:blank') { + // try { + // if (parent.location.origin === location.origin && + // parent.location.href !== location.href) { + // chrome.runtime.connect({name: 'getStyles:' + JSON.stringify(msg)}); + // return true; + // } + // } catch (e) {} + // } + // } function applyOnMessage(request) { if (!chrome.app && document instanceof XMLDocument && request.method !== 'ping') { @@ -320,34 +320,6 @@ return el; } - function setContentsInPageContext() { - try { - (document.head || ROOT).appendChild(document.createElement('script')).text = `(${queue => { - document.currentScript.remove(); - for (const {id, code} of queue) { - const el = document.getElementById(id) || - document.querySelector('style.stylus[id="' + id + '"]'); - if (!el) continue; - const {disabled} = el.sheet; - el.textContent = code; - el.sheet.disabled = disabled; - } - }})(${JSON.stringify(pageContextQueue)})`; - } catch (e) {} - let failedSome; - for (const {el, code} of pageContextQueue) { - if (el.textContent !== code) { - el.textContent = code; - failedSome = true; - } - } - if (failedSome) { - console.debug('Could not set code of some styles in page context, ' + - 'see https://github.com/openstyles/stylus/issues/461'); - } - pageContextQueue.length = 0; - } - function addStyleElement(newElement) { if (!ROOT) { return; @@ -412,14 +384,6 @@ return parseInt(el.id.substr(ID_PREFIX.length)); } - function countStylesInHash(styleHash) { - let num = 0; - for (const k in styleHash) { - num += !isNaN(parseInt(k)) ? 1 : 0; - } - return num; - } - function orphanCheck() { if (chrome.i18n && chrome.i18n.getUILanguage()) { return true; diff --git a/content/install-hook-openusercss.js b/content/install-hook-openusercss.js index 7f438513..31defada 100644 --- a/content/install-hook-openusercss.js +++ b/content/install-hook-openusercss.js @@ -1,3 +1,4 @@ +/* global API */ 'use strict'; (() => { diff --git a/content/install-hook-usercss.js b/content/install-hook-usercss.js index b3e4fba8..622b3143 100644 --- a/content/install-hook-usercss.js +++ b/content/install-hook-usercss.js @@ -1,3 +1,4 @@ +/* global API */ 'use strict'; (() => { diff --git a/content/install-hook-userstyles.js b/content/install-hook-userstyles.js index 94a23cf4..40ed778a 100644 --- a/content/install-hook-userstyles.js +++ b/content/install-hook-userstyles.js @@ -1,4 +1,4 @@ -/* global cloneInto msg */ +/* global cloneInto msg API */ 'use strict'; (() => { diff --git a/edit/applies-to-line-widget.js b/edit/applies-to-line-widget.js index 21d18c30..1d41afbd 100644 --- a/edit/applies-to-line-widget.js +++ b/edit/applies-to-line-widget.js @@ -1,4 +1,6 @@ -/* global regExpTester debounce messageBox CodeMirror template colorMimicry msg */ +/* global regExpTester debounce messageBox CodeMirror template colorMimicry msg + $ $create t prefs tryCatch */ +/* exported createAppliesToLineWidget */ 'use strict'; function createAppliesToLineWidget(cm) { diff --git a/edit/beautify.js b/edit/beautify.js index 617c98f4..80e0e457 100644 --- a/edit/beautify.js +++ b/edit/beautify.js @@ -1,7 +1,5 @@ -/* -global CodeMirror loadScript css_beautify -global getSectionForChild showHelp -*/ +/* global loadScript css_beautify showHelp prefs t $ $create */ +/* exported beautify */ 'use strict'; function beautify(scope) { diff --git a/edit/codemirror-default.js b/edit/codemirror-default.js index aa6e2f14..e92be6e4 100644 --- a/edit/codemirror-default.js +++ b/edit/codemirror-default.js @@ -1,4 +1,4 @@ -/* global CodeMirror prefs loadScript editor */ +/* global CodeMirror prefs loadScript editor $ template */ 'use strict'; diff --git a/edit/codemirror-factory.js b/edit/codemirror-factory.js index 0773d0f9..cb5e4586 100644 --- a/edit/codemirror-factory.js +++ b/edit/codemirror-factory.js @@ -1,4 +1,5 @@ -/* global CodeMirror loadScript rerouteHotkeys */ +/* global CodeMirror loadScript rerouteHotkeys prefs $ debounce $create */ +/* exported cmFactory */ 'use strict'; /* All cm instances created by this module are collected so we can broadcast prefs diff --git a/edit/colorpicker-helper.js b/edit/colorpicker-helper.js index 0c28939e..cae28510 100644 --- a/edit/colorpicker-helper.js +++ b/edit/colorpicker-helper.js @@ -1,4 +1,4 @@ -/* global CodeMirror loadScript showHelp cmFactory */ +/* global CodeMirror showHelp cmFactory onDOMready $ $create prefs t */ 'use strict'; (() => { diff --git a/edit/edit.js b/edit/edit.js index 9420d1c3..44962cb8 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -1,16 +1,13 @@ -/* -global CodeMirror loadScript -global createSourceEditor -global closeCurrentTab regExpTester messageBox -global setupCodeMirror -global beautify -global sectionsToMozFormat -global moveFocus editorWorker msg createSectionsEditor rerouteHotkeys +/* global CodeMirror onDOMready prefs setupLivePrefs $ $$ $create t tHTML + createSourceEditor queryTabs sessionStorageHash getOwnTab FIREFOX API tryCatch + closeCurrentTab messageBox debounce + beautify + moveFocus msg createSectionsEditor rerouteHotkeys */ +/* exported showCodeMirrorPopup */ 'use strict'; let saveSizeOnClose; -let ownTabId; // direct & reverse mapping of @-moz-document keywords and internal property names const propertyToCss = {urls: 'url', urlPrefixes: 'url-prefix', domains: 'domain', regexps: 'regexp'}; @@ -242,29 +239,6 @@ prefs.subscribe(['editor.smartIndent'], (key, value) => CodeMirror.setOption('smartIndent', value)); function preinit() { - // make querySelectorAll enumeration code readable - // FIXME: don't extend native - ['forEach', 'some', 'indexOf', 'map'].forEach(method => { - NodeList.prototype[method] = Array.prototype[method]; - }); - - // eslint-disable-next-line no-extend-native - Object.defineProperties(Array.prototype, { - last: { - get() { - return this[this.length - 1]; - }, - }, - rotate: { - value: function (amount) { - // negative amount == rotate left - const r = this.slice(-amount, this.length); - Array.prototype.push.apply(r, this.slice(0, this.length - r.length)); - return r; - }, - }, - }); - // preload the theme so that CodeMirror can calculate its metrics in DOMContentLoaded->setupLivePrefs() new MutationObserver((mutations, observer) => { const themeElement = $('#cm-theme'); @@ -302,7 +276,7 @@ function preinit() { } getOwnTab().then(tab => { - ownTabId = tab.id; + const ownTabId = tab.id; // use browser history back when 'back to manage' is clicked if (sessionStorageHash('manageStylesHistory').value[ownTabId] === location.href) { @@ -446,21 +420,6 @@ function initStyleData() { } } -function showSectionHelp(event) { - event.preventDefault(); - showHelp(t('styleSectionsTitle'), t('sectionHelp')); -} - -function showAppliesToHelp(event) { - event.preventDefault(); - showHelp(t('appliesLabel'), t('appliesHelp')); -} - -function showToMozillaHelp(event) { - event.preventDefault(); - showHelp(t('styleMozillaFormatHeading'), t('styleToMozillaFormatHelp')); -} - function showHelp(title = '', body) { const div = $('#help-popup'); div.className = ''; @@ -559,20 +518,6 @@ function showCodeMirrorPopup(title, html, options) { return popup; } -function setGlobalProgress(done, total) { - const progressElement = $('#global-progress') || - total && document.body.appendChild($create('#global-progress')); - if (total) { - const progress = (done / Math.max(done, total) * 100).toFixed(1); - progressElement.style.borderLeftWidth = progress + 'vw'; - setTimeout(() => { - progressElement.title = progress + '%'; - }); - } else { - $.remove(progressElement); - } -} - function hideLintHeaderOnScroll() { // workaround part2 for the
not showing its toggle icon: hide on scroll const newOpacity = this.scrollTop === 0 ? '' : '0'; @@ -610,14 +555,3 @@ function isWindowMaximized() { window.outerHeight < screen.availHeight + 10 ); } - -function toggleContextMenuDelete(event) { - if (chrome.contextMenus && event.button === 2 && prefs.get('editor.contextDelete')) { - chrome.contextMenus.update('editor.contextDelete', { - enabled: Boolean( - this.selectionStart !== this.selectionEnd || - this.somethingSelected && this.somethingSelected() - ), - }, ignoreChromeError); - } -} diff --git a/edit/editor-worker.js b/edit/editor-worker.js index 1323ea72..aaf4bd46 100644 --- a/edit/editor-worker.js +++ b/edit/editor-worker.js @@ -1,3 +1,4 @@ +/* exported editorWorker */ 'use strict'; // eslint-disable-next-line no-var diff --git a/js/exclusions.js b/edit/exclusions.js similarity index 94% rename from js/exclusions.js rename to edit/exclusions.js index aec673e3..4e143949 100644 --- a/js/exclusions.js +++ b/edit/exclusions.js @@ -1,8 +1,4 @@ -/* -global messageBox resolveWith -gloabl editor showHelp getSectionsHashes -global popupExclusions -*/ +/* global showHelp $ $$ debounce API template t */ 'use strict'; const exclusions = (() => { @@ -142,9 +138,9 @@ const exclusions = (() => { if (msg.method === 'exclusionsUpdated' && msg.style && msg.style.exclusions) { update({list: Object.keys(msg.style.exclusions), isUpdating: true}); // update popup, if loaded - if (typeof popupExclusions !== 'undefined') { - popupExclusions.selectExclusions(msg.style.exclusions); - } + // if (typeof popupExclusions !== 'undefined') { + // popupExclusions.selectExclusions(msg.style.exclusions); + // } } } @@ -162,7 +158,6 @@ const exclusions = (() => { style.exclusions = exclusionList; style.reason = 'exclusionsUpdated'; API.saveStyle(style); - notifyAllTabs({method: 'exclusionsUpdated', style, id, excluded: exclusionList}); }); } diff --git a/edit/global-search.js b/edit/global-search.js index d364ec60..c160de16 100644 --- a/edit/global-search.js +++ b/edit/global-search.js @@ -1,6 +1,5 @@ -/* global CodeMirror */ -/* global focusAccessibility */ -/* global colorMimicry editor */ +/* global CodeMirror focusAccessibility colorMimicry editor + onDOMready $ $$ $create t debounce tryRegExp stringAsRegExp template */ 'use strict'; onDOMready().then(() => { diff --git a/edit/linter-config-dialog.js b/edit/linter-config-dialog.js index c06ce9ba..310bf2b5 100644 --- a/edit/linter-config-dialog.js +++ b/edit/linter-config-dialog.js @@ -1,5 +1,6 @@ /* global memoize editorWorker showCodeMirrorPopup loadScript messageBox - LINTER_DEFAULTS rerouteHotkeys */ + LINTER_DEFAULTS rerouteHotkeys $ $create $createLink tryJSONparse t + chromeSync */ 'use strict'; (() => { diff --git a/edit/linter-defaults.js b/edit/linter-defaults.js index e02e5420..a004b2b8 100644 --- a/edit/linter-defaults.js +++ b/edit/linter-defaults.js @@ -1,7 +1,7 @@ +/* exported LINTER_DEFAULTS */ 'use strict'; -// eslint-disable-next-line no-var -var LINTER_DEFAULTS = (() => { +const LINTER_DEFAULTS = (() => { const SEVERITY = {severity: 'warning'}; const STYLELINT = { // 'sugarss' is a indent-based syntax like Sass or Stylus diff --git a/edit/linter-engines.js b/edit/linter-engines.js index f884ceda..7f7717b8 100644 --- a/edit/linter-engines.js +++ b/edit/linter-engines.js @@ -1,4 +1,4 @@ -/* global LINTER_DEFAULTS linter editorWorker */ +/* global LINTER_DEFAULTS linter editorWorker prefs chromeSync */ 'use strict'; (() => { diff --git a/edit/linter-help-dialog.js b/edit/linter-help-dialog.js index 7946a6e5..f1c6d18c 100644 --- a/edit/linter-help-dialog.js +++ b/edit/linter-help-dialog.js @@ -1,4 +1,5 @@ -/* global showHelp editorWorker memoize */ +/* global showHelp editorWorker memoize $ $create $createLink t */ +/* exported createLinterHelpDialog */ 'use strict'; function createLinterHelpDialog(getIssues) { diff --git a/edit/linter-meta.js b/edit/linter-meta.js index 4233c402..b453df9c 100644 --- a/edit/linter-meta.js +++ b/edit/linter-meta.js @@ -1,4 +1,5 @@ -/* global linter */ +/* global linter API */ +/* exported createMetaCompiler */ 'use strict'; function createMetaCompiler(cm) { diff --git a/edit/linter-report.js b/edit/linter-report.js index 7dc74f0d..df7fda83 100644 --- a/edit/linter-report.js +++ b/edit/linter-report.js @@ -1,7 +1,6 @@ -/* global linter editor clipString createLinterHelpDialog makeSectionVisible */ +/* global linter editor clipString createLinterHelpDialog $ $create */ 'use strict'; -// eslint-disable-next-line no-var Object.assign(linter, (() => { const tables = new Map(); const helpDialog = createLinterHelpDialog(getIssues); diff --git a/edit/linter.js b/edit/linter.js index 5cb7f779..a8d927fa 100644 --- a/edit/linter.js +++ b/edit/linter.js @@ -1,7 +1,8 @@ +/* global prefs */ 'use strict'; -// eslint-disable-next-line no-var -var linter = (() => { +/* exported linter */ +const linter = (() => { const lintingUpdatedListeners = []; const unhookListeners = []; const linters = []; diff --git a/edit/live-preview.js b/edit/live-preview.js index f02a5cbd..c2ff3ca4 100644 --- a/edit/live-preview.js +++ b/edit/live-preview.js @@ -1,4 +1,5 @@ -/* global messageBox editor */ +/* global messageBox editor $ prefs */ +/* exported createLivePreview */ 'use strict'; function createLivePreview(preprocess) { diff --git a/edit/match-highlighter-helper.js b/edit/match-highlighter-helper.js index 34e598a4..8396a7ed 100644 --- a/edit/match-highlighter-helper.js +++ b/edit/match-highlighter-helper.js @@ -1,4 +1,4 @@ -/* global CodeMirror */ +/* global CodeMirror prefs */ 'use strict'; (() => { diff --git a/edit/regexp-tester.js b/edit/regexp-tester.js index 09c4125d..4ba609ba 100644 --- a/edit/regexp-tester.js +++ b/edit/regexp-tester.js @@ -1,8 +1,8 @@ -/* global showHelp */ +/* global showHelp $ $create tryRegExp queryTabs URLS t template openURL */ +/* exported regExpTester */ 'use strict'; -// eslint-disable-next-line no-var -var regExpTester = (() => { +const regExpTester = (() => { const GET_FAVICON_URL = 'https://www.google.com/s2/favicons?domain='; const OWN_ICON = chrome.runtime.getManifest().icons['16']; const cachedRegexps = new Map(); diff --git a/edit/reroute-hotkeys.js b/edit/reroute-hotkeys.js index 75945b99..5ca2f149 100644 --- a/edit/reroute-hotkeys.js +++ b/edit/reroute-hotkeys.js @@ -1,4 +1,5 @@ -/* global CodeMirror editor */ +/* global CodeMirror editor debounce */ +/* exported rerouteHotkeys */ 'use strict'; const rerouteHotkeys = (() => { diff --git a/edit/sections-editor.js b/edit/sections-editor.js index db2f2be1..b107361f 100644 --- a/edit/sections-editor.js +++ b/edit/sections-editor.js @@ -1,10 +1,11 @@ -/* global dirtyReporter showToMozillaHelp - showSectionHelp toggleContextMenuDelete setGlobalProgress maximizeCodeHeight - CodeMirror nextPrevEditorOnKeydown showAppliesToHelp propertyToCss +/* global dirtyReporter showHelp prefs ignoreChromeError + CodeMirror propertyToCss regExpTester linter createLivePreview showCodeMirrorPopup sectionsToMozFormat editorWorker messageBox clipString beautify - rerouteHotkeys cmFactory CssToProperty + rerouteHotkeys cmFactory CssToProperty template $ $$ $create t FIREFOX API + debounce tryRegExp */ +/* exported createSectionsEditor */ 'use strict'; function createResizeGrip(cm) { @@ -98,6 +99,7 @@ function createSectionsEditor(style) { $('#to-mozilla-help').addEventListener('click', showToMozillaHelp); $('#from-mozilla').addEventListener('click', () => fromMozillaFormat()); $('#save-button').addEventListener('click', saveStyle); + // FIXME: this element doesn't exist? $('#sections-help').addEventListener('click', showSectionHelp); document.addEventListener('wheel', scrollEntirePageOnCtrlShift); @@ -148,6 +150,46 @@ function createSectionsEditor(style) { getSearchableInputs, }; + function toggleContextMenuDelete(event) { + if (chrome.contextMenus && event.button === 2 && prefs.get('editor.contextDelete')) { + chrome.contextMenus.update('editor.contextDelete', { + enabled: Boolean( + this.selectionStart !== this.selectionEnd || + this.somethingSelected && this.somethingSelected() + ), + }, ignoreChromeError); + } + } + + function setGlobalProgress(done, total) { + const progressElement = $('#global-progress') || + total && document.body.appendChild($create('#global-progress')); + if (total) { + const progress = (done / Math.max(done, total) * 100).toFixed(1); + progressElement.style.borderLeftWidth = progress + 'vw'; + setTimeout(() => { + progressElement.title = progress + '%'; + }); + } else { + $.remove(progressElement); + } + } + + function showToMozillaHelp(event) { + event.preventDefault(); + showHelp(t('styleMozillaFormatHeading'), t('styleToMozillaFormatHelp')); + } + + function showAppliesToHelp(event) { + event.preventDefault(); + showHelp(t('appliesLabel'), t('appliesHelp')); + } + + function showSectionHelp(event) { + event.preventDefault(); + showHelp(t('styleSectionsTitle'), t('sectionHelp')); + } + function getSearchableInputs(cm) { return sections.find(s => s.cm === cm).appliesTo.map(a => a.valueEl).filter(Boolean); } diff --git a/edit/show-keymap-help.js b/edit/show-keymap-help.js index 66dc710a..62481787 100644 --- a/edit/show-keymap-help.js +++ b/edit/show-keymap-help.js @@ -1,4 +1,5 @@ -/* global CodeMirror showHelp */ +/* global CodeMirror showHelp onDOMready $ $$ $create template t + prefs stringAsRegExp */ 'use strict'; onDOMready().then(() => { diff --git a/edit/source-editor.js b/edit/source-editor.js index 107a8c5c..c0476f80 100644 --- a/edit/source-editor.js +++ b/edit/source-editor.js @@ -1,9 +1,9 @@ -/* -global CodeMirror dirtyReporter -global createAppliesToLineWidget messageBox -global sectionsToMozFormat -global createMetaCompiler linter createLivePreview cmFactory -*/ +/* global dirtyReporter + createAppliesToLineWidget messageBox + sectionsToMozFormat + createMetaCompiler linter createLivePreview cmFactory $ $create API prefs t + chromeSync */ +/* exported createSourceEditor */ 'use strict'; function createSourceEditor(style) { diff --git a/edit/util.js b/edit/util.js index a16c742f..cc18b515 100644 --- a/edit/util.js +++ b/edit/util.js @@ -1,3 +1,4 @@ +/* exported dirtyReporter memoize clipString sectionsToMozFormat */ 'use strict'; function dirtyReporter() { diff --git a/install-usercss/install-usercss.js b/install-usercss/install-usercss.js index 7164c8de..8b375e60 100644 --- a/install-usercss/install-usercss.js +++ b/install-usercss/install-usercss.js @@ -1,5 +1,5 @@ -/* global CodeMirror semverCompare closeCurrentTab */ -/* global messageBox download chromeLocal */ +/* global CodeMirror semverCompare closeCurrentTab messageBox download + $ $$ $create $createLink t prefs API getTab */ 'use strict'; (() => { diff --git a/js/cache.js b/js/cache.js index 31e3df6e..f86f4331 100644 --- a/js/cache.js +++ b/js/cache.js @@ -1,5 +1,7 @@ +/* exported createCache */ 'use strict'; +// create a FIFO limit-size map. function createCache(size = 1000) { const map = new Map(); const buffer = Array(size); diff --git a/js/dom.js b/js/dom.js index 7ffb9ce6..a1ae0180 100644 --- a/js/dom.js +++ b/js/dom.js @@ -1,9 +1,35 @@ +/* global prefs */ +/* exported scrollElementIntoView animateElement enforceInputRange $createLink + setupLivePrefs moveFocus */ 'use strict'; if (!/^Win\d+/.test(navigator.platform)) { document.documentElement.classList.add('non-windows'); } +// make querySelectorAll enumeration code readable +// FIXME: avoid extending native? +['forEach', 'some', 'indexOf', 'map'].forEach(method => { + NodeList.prototype[method] = Array.prototype[method]; +}); + +// eslint-disable-next-line no-extend-native +Object.defineProperties(Array.prototype, { + last: { + get() { + return this[this.length - 1]; + }, + }, + rotate: { + value: function (amount) { + // negative amount == rotate left + const r = this.slice(-amount, this.length); + Array.prototype.push.apply(r, this.slice(0, this.length - r.length)); + return r; + }, + }, +}); + // polyfill for old browsers to enable [...results] and for-of for (const type of [NodeList, NamedNodeMap, HTMLCollection, HTMLAllCollection]) { if (!type.prototype[Symbol.iterator]) { diff --git a/js/localization.js b/js/localization.js index b33c3c29..e4a8de10 100644 --- a/js/localization.js +++ b/js/localization.js @@ -1,3 +1,5 @@ +/* global tryCatch */ +/* exported tHTML formatDate */ 'use strict'; const template = {}; diff --git a/js/messaging.js b/js/messaging.js index fc1cb78a..e8335bd9 100644 --- a/js/messaging.js +++ b/js/messaging.js @@ -1,14 +1,13 @@ -/* -global BG: true -global FIREFOX: true -global onRuntimeMessage applyOnMessage -*/ +/* exported getActiveTab onTabReady stringAsRegExp getTabRealURL openURL + getStyleWithNoCode tryRegExp sessionStorageHash download + closeCurrentTab */ 'use strict'; const CHROME = Boolean(chrome.app) && parseInt(navigator.userAgent.match(/Chrom\w+\/(?:\d+\.){2}(\d+)|$/)[1]); const OPERA = Boolean(chrome.app) && parseFloat(navigator.userAgent.match(/\bOPR\/(\d+\.\d+)|$/)[1]); const VIVALDI = Boolean(chrome.app) && navigator.userAgent.includes('Vivaldi'); -const ANDROID = !chrome.windows; +// FIXME: who use this? +// const ANDROID = !chrome.windows; let FIREFOX = !chrome.app && parseFloat(navigator.userAgent.match(/\bFirefox\/(\d+\.\d+)|$/)[1]); if (!CHROME && !chrome.browserAction.openPopup) { @@ -120,13 +119,6 @@ function getActiveTab() { .then(tabs => tabs[0]); } - -function getActiveTabRealURL() { - return getActiveTab() - .then(getTabRealURL); -} - - function getTabRealURL(tab) { return new Promise(resolve => { if (tab.url !== 'chrome://newtab/' || URLS.chromeProtectsNTP) { @@ -487,27 +479,6 @@ function download(url, { } } - -function invokeOrPostpone(isInvoke, fn, ...args) { - return isInvoke - ? fn(...args) - : setTimeout(invokeOrPostpone, 0, true, fn, ...args); -} - - -function openEditor({id}) { - let url = '/edit.html'; - if (id) { - url += `?id=${id}`; - } - if (chrome.windows && prefs.get('openEditInWindow')) { - chrome.windows.create(Object.assign({url}, prefs.get('windowPosition'))); - } else { - openURL({url}); - } -} - - function closeCurrentTab() { // https://bugzilla.mozilla.org/show_bug.cgi?id=1409375 getOwnTab().then(tab => { @@ -516,6 +487,3 @@ function closeCurrentTab() { } }); } - -// FIXME: remove this when #510 is merged -function notifyAllTabs() {} diff --git a/js/moz-parser.js b/js/moz-parser.js index f5e7eb8e..32a73afb 100644 --- a/js/moz-parser.js +++ b/js/moz-parser.js @@ -1,4 +1,5 @@ /* global parserlib */ +/* exported parseMozFormat */ 'use strict'; /** diff --git a/js/msg.js b/js/msg.js index 20b17585..afc6d66a 100644 --- a/js/msg.js +++ b/js/msg.js @@ -1,4 +1,5 @@ /* global promisify deepCopy */ +/* exported msg API */ // deepCopy is only used if the script is executed in extension pages. 'use strict'; diff --git a/js/prefs.js b/js/prefs.js index f798a96a..cf0776ef 100644 --- a/js/prefs.js +++ b/js/prefs.js @@ -1,4 +1,3 @@ -/* global deepCopy debounce API tryJSONparse chromeSync FIREFOX CHROME */ /* exported prefs */ 'use strict'; diff --git a/js/storage-util.js b/js/storage-util.js index 34924f55..044ba56a 100644 --- a/js/storage-util.js +++ b/js/storage-util.js @@ -1,4 +1,4 @@ -/* global loadScript tryJSONparse API */ +/* global loadScript tryJSONparse */ /* exported chromeLocal chromeSync */ 'use strict';