diff --git a/content/install.js b/content/install.js index f8bba479..89600266 100644 --- a/content/install.js +++ b/content/install.js @@ -22,14 +22,14 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { // TODO: remove the following statement when USO is fixed document.documentElement.appendChild(document.createElement('script')).text = '(' + - function() { + function () { let settings; document.addEventListener('stylusFixBuggyUSOsettings', function _({detail}) { document.removeEventListener('stylusFixBuggyUSOsettings', _); settings = /\?/.test(detail) && new URLSearchParams(new URL(detail).search); }); const originalResponseJson = Response.prototype.json; - Response.prototype.json = function(...args) { + Response.prototype.json = function (...args) { return originalResponseJson.call(this, ...args).then(json => { Response.prototype.json = originalResponseJson; if (!settings || typeof ((json || {}).style_settings || {}).every != 'function') { diff --git a/edit/edit.js b/edit/edit.js index 53053f95..6eb0406b 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -16,7 +16,7 @@ const CssToProperty = {'url': 'urls', 'url-prefix': 'urlPrefixes', 'domain': 'do onBackgroundReady(); // make querySelectorAll enumeration code readable -['forEach', 'some', 'indexOf', 'map'].forEach(function(method) { +['forEach', 'some', 'indexOf', 'map'].forEach(method => { NodeList.prototype[method] = Array.prototype[method]; }); @@ -24,7 +24,7 @@ onBackgroundReady(); Element.prototype.matches = Element.prototype.matches || Element.prototype.webkitMatchesSelector; // Chrome pre-41 polyfill -Element.prototype.closest = Element.prototype.closest || function(selector) { +Element.prototype.closest = Element.prototype.closest || function (selector) { let e; // eslint-disable-next-line no-empty for (e = this; e && !e.matches(selector); e = e.parentElement) {} @@ -32,14 +32,14 @@ Element.prototype.closest = Element.prototype.closest || function(selector) { }; // eslint-disable-next-line no-extend-native -Array.prototype.rotate = function(amount) { // negative amount == rotate left +Array.prototype.rotate = 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; }; // eslint-disable-next-line no-extend-native -Object.defineProperty(Array.prototype, 'last', {get: function() { return this[this.length - 1]; }}); +Object.defineProperty(Array.prototype, 'last', {get: function () { return this[this.length - 1]; }}); // preload the theme so that CodeMirror can calculate its metrics in DOMContentLoaded->setupLivePrefs() new MutationObserver((mutations, observer) => { @@ -60,12 +60,12 @@ const hotkeyRerouter = { find: true, findNext: true, findPrev: true, replace: true, replaceAll: true, toggleStyle: true, }, - setState: function(enable) { - setTimeout(function() { + setState: enable => { + setTimeout(() => { document[(enable ? 'add' : 'remove') + 'EventListener']('keydown', hotkeyRerouter.eventHandler); }, 0); }, - eventHandler: function(event) { + eventHandler: event => { const keyName = CodeMirror.keyName(event); if ( CodeMirror.lookupKey(keyName, CodeMirror.getOption('keyMap'), handleCommand) == 'handled' || @@ -139,7 +139,7 @@ function setCleanGlobal() { } function setCleanSection(section) { - section.querySelectorAll('.style-contributor').forEach(function(node) { setCleanItem(node, true); }); + section.querySelectorAll('.style-contributor').forEach(node => { setCleanItem(node, true); }); // #header section has no codemirror const cm = section.CodeMirror; @@ -182,10 +182,10 @@ function initCodeMirror() { // additional commands CM.commands.jumpToLine = jumpToLine; - CM.commands.nextEditor = function(cm) { nextPrevEditor(cm, 1); }; - CM.commands.prevEditor = function(cm) { nextPrevEditor(cm, -1); }; + CM.commands.nextEditor = cm => { nextPrevEditor(cm, 1); }; + CM.commands.prevEditor = cm => { nextPrevEditor(cm, -1); }; CM.commands.save = save; - CM.commands.blockComment = function(cm) { + CM.commands.blockComment = cm => { cm.blockComment(cm.getCursor('from'), cm.getCursor('to'), {fullLines: false}); }; CM.commands.toggleStyle = toggleStyle; @@ -193,7 +193,7 @@ function initCodeMirror() { // 'basic' keymap only has basic keys by design, so we skip it const extraKeysCommands = {}; - Object.keys(CM.defaults.extraKeys).forEach(function(key) { + Object.keys(CM.defaults.extraKeys).forEach(key => { extraKeysCommands[CM.defaults.extraKeys[key]] = true; }); if (!extraKeysCommands.jumpToLine) { @@ -222,18 +222,18 @@ function initCodeMirror() { } // try to remap non-interceptable Ctrl-(Shift-)N/T/W hotkeys - ['N', 'T', 'W'].forEach(function(char) { + ['N', 'T', 'W'].forEach(char => { [{from: 'Ctrl-', to: ['Alt-', 'Ctrl-Alt-']}, {from: 'Shift-Ctrl-', to: ['Ctrl-Alt-', 'Shift-Ctrl-Alt-']} // Note: modifier order in CM is S-C-A - ].forEach(function(remap) { + ].forEach(remap => { const oldKey = remap.from + char; - Object.keys(CM.keyMap).forEach(function(keyMapName) { + Object.keys(CM.keyMap).forEach(keyMapName => { const keyMap = CM.keyMap[keyMapName]; const command = keyMap[oldKey]; if (!command) { return; } - remap.to.some(function(newMod) { + remap.to.some(newMod => { const newKey = newMod + char; if (!(newKey in keyMap)) { delete keyMap[oldKey]; @@ -247,23 +247,21 @@ function initCodeMirror() { } // user option values - CM.getOption = function(o) { - return CodeMirror.defaults[o]; - }; - CM.setOption = function(o, v) { + CM.getOption = o => CodeMirror.defaults[o]; + CM.setOption = (o, v) => { CodeMirror.defaults[o] = v; - editors.forEach(function(editor) { + editors.forEach(editor => { editor.setOption(o, v); }); }; - CM.prototype.getSection = function() { + CM.prototype.getSection = function () { return this.display.wrapper.parentNode; }; // initialize global editor controls function optionsHtmlFromArray(options) { - return options.map(function(opt) { return ''; }).join(''); + return options.map(opt => '').join(''); } const themeControl = document.getElementById('editor.theme'); const themeList = localStorage.codeMirrorThemes; @@ -318,8 +316,8 @@ function acmeEventListener(event) { // avoid flicker: wait for the second stylesheet to load, then apply the theme document.head.insertAdjacentHTML('beforeend', ''); - (function() { - setTimeout(function() { + (() => { + setTimeout(() => { CodeMirror.setOption(option, value); themeLink.remove(); document.getElementById('cm-theme2').id = 'cm-theme'; @@ -426,13 +424,13 @@ function getSections() { // remind Chrome to repaint a previously invisible editor box by toggling any element's transform // this bug is present in some versions of Chrome (v37-40 or something) -document.addEventListener('scroll', function() { +document.addEventListener('scroll', () => { const style = document.getElementById('name').style; style.webkitTransform = style.webkitTransform ? '' : 'scale(1)'; }); // Shift-Ctrl-Wheel scrolls entire page even when mouse is over a code editor -document.addEventListener('wheel', function(event) { +document.addEventListener('wheel', event => { if (event.shiftKey && event.ctrlKey && !event.altKey && !event.metaKey) { // Chrome scrolls horizontally when Shift is pressed but on some PCs this might be different window.scrollBy(0, event.deltaX || event.deltaY); @@ -450,7 +448,7 @@ queryTabs({currentWindow: true}).then(tabs => { chrome.windows.update(windowId, prefs.get('windowPosition')); } if (tabs.length == 1 && window.history.length == 1) { - chrome.windows.getAll(function(windows) { + chrome.windows.getAll(windows => { if (windows.length > 1) { sessionStorageHash('saveSizeOnClose').set(windowId, true); saveSizeOnClose = true; @@ -460,7 +458,7 @@ queryTabs({currentWindow: true}).then(tabs => { saveSizeOnClose = sessionStorageHash('saveSizeOnClose').value[windowId]; } } - chrome.tabs.onRemoved.addListener(function(tabId, info) { + chrome.tabs.onRemoved.addListener((tabId, info) => { sessionStorageHash('manageStylesHistory').unset(tabId); if (info.windowId == windowId && info.isWindowClosing) { sessionStorageHash('saveSizeOnClose').unset(windowId); @@ -489,7 +487,7 @@ function isWindowMaximized() { && window.outerHeight == screen.availHeight; } -window.onbeforeunload = function() { +window.onbeforeunload = () => { if (saveSizeOnClose && !isWindowMaximized()) { prefs.set('windowPosition', { left: screenLeft, @@ -527,7 +525,7 @@ function addAppliesTo(list, name, value) { } else { e = template.appliesToEverything.cloneNode(true); } - e.querySelector('.add-applies-to').addEventListener('click', function() { + e.querySelector('.add-applies-to').addEventListener('click', function () { addAppliesTo(this.parentNode.parentNode); }, false); list.appendChild(e); @@ -548,7 +546,7 @@ function addSection(event, section) { codeElement.value = section.code; for (const i in propertyToCss) { if (section[i]) { - section[i].forEach(function(url) { + section[i].forEach(url => { addAppliesTo(appliesTo, propertyToCss[i], url); appliesToAdded = true; }); @@ -621,7 +619,7 @@ function removeAreaAndSetDirty(area) { if (!contributors.length) { setCleanItem(area, false); } - contributors.some(function(node) { + contributors.some(node => { if (node.savedValue) { // it's a saved section, so make it dirty and stop the enumeration setCleanItem(area, false); @@ -688,11 +686,11 @@ function setupGlobalSearch() { // temporarily overrides the original openDialog with the provided template's innerHTML function customizeOpenDialog(cm, template, callback) { - cm.openDialog = function(tmpl, cb, opt) { + cm.openDialog = (tmpl, cb, opt) => { // invoke 'callback' and bind 'this' to the original callback originalOpenDialog.call(cm, template.innerHTML, callback.bind(cb), opt); }; - setTimeout(function() { cm.openDialog = originalOpenDialog; }, 0); + setTimeout(() => { cm.openDialog = originalOpenDialog; }, 0); refocusMinidialog(cm); } @@ -707,13 +705,13 @@ function setupGlobalSearch() { function find(activeCM) { activeCM = focusClosestCM(activeCM); - customizeOpenDialog(activeCM, template.find, function(query) { + customizeOpenDialog(activeCM, template.find, function (query) { this(query); curState = activeCM.state.search; if (editors.length == 1 || !curState.query) { return; } - editors.forEach(function(cm) { + editors.forEach(cm => { if (cm != activeCM) { cm.execCommand('clearSearch'); updateState(cm, curState); @@ -776,14 +774,14 @@ function setupGlobalSearch() { inputs = inputs.reverse(); } inputs.splice(0, inputs.indexOf(document.activeElement) + 1); - return inputs.some(function(input) { + return inputs.some(input => { const match = rxQuery.exec(input.value); if (match) { input.focus(); const end = match.index + match[0].length; // scroll selected part into view in long inputs, // works only outside of current event handlers chain, hence timeout=0 - setTimeout(function() { + setTimeout(() => { input.setSelectionRange(end, end); input.setSelectionRange(match.index, end); }, 0); @@ -802,9 +800,9 @@ function setupGlobalSearch() { let query; let replacement; activeCM = focusClosestCM(activeCM); - customizeOpenDialog(activeCM, template[all ? 'replaceAll' : 'replace'], function(txt) { + customizeOpenDialog(activeCM, template[all ? 'replaceAll' : 'replace'], txt => { query = txt; - customizeOpenDialog(activeCM, template.replaceWith, function(txt) { + customizeOpenDialog(activeCM, template.replaceWith, txt => { replacement = txt; queue = editors.rotate(-editors.indexOf(activeCM)); if (all) { @@ -826,8 +824,8 @@ function setupGlobalSearch() { return; } // hide the first two dialogs (replace, replaceWith) - cm.openDialog = function(tmpl, callback, opt) { - cm.openDialog = function(tmpl, callback, opt) { + cm.openDialog = (tmpl, callback, opt) => { + cm.openDialog = (tmpl, callback, opt) => { cm.openDialog = originalOpenDialog; if (all) { callback(replacement); @@ -848,25 +846,23 @@ function setupGlobalSearch() { let wrapAround = false; const origPos = cm.getCursor(); cm.openConfirm = function overrideConfirm(tmpl, callbacks, opt) { - const ovrCallbacks = callbacks.map(function(callback) { - return function() { - makeSectionVisible(cm); - cm.openConfirm = overrideConfirm; - setTimeout(function() { cm.openConfirm = originalOpenConfirm; }, 0); + const ovrCallbacks = callbacks.map(callback => () => { + makeSectionVisible(cm); + cm.openConfirm = overrideConfirm; + setTimeout(() => { cm.openConfirm = originalOpenConfirm; }, 0); - const pos = cm.getCursor(); - callback(); - const cmp = CodeMirror.cmpPos(cm.getCursor(), pos); - wrapAround |= cmp <= 0; + const pos = cm.getCursor(); + callback(); + const cmp = CodeMirror.cmpPos(cm.getCursor(), pos); + wrapAround |= cmp <= 0; - const dlg = cm.getWrapperElement().querySelector('.CodeMirror-dialog'); - if (!dlg || cmp == 0 || wrapAround && CodeMirror.cmpPos(cm.getCursor(), origPos) >= 0) { - if (dlg) { - dlg.remove(); - } - doReplace(); + const dlg = cm.getWrapperElement().querySelector('.CodeMirror-dialog'); + if (!dlg || cmp == 0 || wrapAround && CodeMirror.cmpPos(cm.getCursor(), origPos) >= 0) { + if (dlg) { + dlg.remove(); } - }; + doReplace(); + } }); originalOpenConfirm.call(cm, template.replaceConfirm.innerHTML, ovrCallbacks, opt); }; @@ -887,7 +883,7 @@ function setupGlobalSearch() { function jumpToLine(cm) { const cur = cm.getCursor(); refocusMinidialog(cm); - cm.openDialog(template.jumpToLine.innerHTML, function(str) { + cm.openDialog(template.jumpToLine.innerHTML, str => { const m = str.match(/^\s*(\d+)(?:\s*:\s*(\d+))?\s*$/); if (m) { cm.setCursor(m[1] - 1, m[2] ? m[2] - 1 : cur.ch); @@ -959,7 +955,7 @@ function refocusMinidialog(cm) { // close the currently opened minidialog cm.focus(); // make sure to focus the input in newly opened minidialog - setTimeout(function() { + setTimeout(() => { section.querySelector('.CodeMirror-dialog').focus(); }, 0); } @@ -980,8 +976,8 @@ function getEditorInSight(nearbyElement) { } if (!cm || offscreenDistance(cm) > 0) { const sorted = editors - .map(function(cm, index) { return {cm: cm, distance: offscreenDistance(cm), index: index}; }) - .sort(function(a, b) { return a.distance - b.distance || a.index - b.index; }); + .map((cm, index) => ({cm: cm, distance: offscreenDistance(cm), index: index})) + .sort((a, b) => a.distance - b.distance || a.index - b.index); cm = sorted[0].cm; if (sorted[0].distance > 0) { makeSectionVisible(cm); @@ -1027,12 +1023,12 @@ function updateLintReport(cm, delay) { const scope = cm ? [cm] : editors; let changed = false; let fixedOldIssues = false; - scope.forEach(function(cm) { + scope.forEach(cm => { const scopedState = cm.state.lint || {}; const oldMarkers = scopedState.markedLast || {}; const newMarkers = {}; const html = !scopedState.marked || scopedState.marked.length == 0 ? '' : '
' + - scopedState.marked.map(function(mark) { + scopedState.marked.map(mark => { const info = mark.__annotation; const isActiveLine = info.from.line == cm.getCursor().line; const pos = isActiveLine ? 'cursor' : (info.from.line + ',' + info.from.ch); @@ -1064,7 +1060,7 @@ function updateLintReport(cm, delay) { if (!state || !state.postponeNewIssues || fixedOldIssues) { renderLintReport(true); } else { - state.renderTimeout = setTimeout(function() { + state.renderTimeout = setTimeout(() => { renderLintReport(true); }, CodeMirror.defaults.lintReportDelay); } @@ -1072,7 +1068,7 @@ function updateLintReport(cm, delay) { } function escapeHtml(html) { const chars = {'&': '&', '<': '<', '>': '>', '"': '"', "'": ''', '/': '/'}; - return html.replace(/[&<>"'/]/g, function(char) { return chars[char]; }); + return html.replace(/[&<>"'/]/g, char => chars[char]); } } @@ -1082,7 +1078,7 @@ function renderLintReport(someBlockChanged) { const label = t('sectionCode'); const newContent = content.cloneNode(false); let issueCount = 0; - editors.forEach(function(cm, index) { + editors.forEach((cm, index) => { if (cm.state.lint && cm.state.lint.html) { const newBlock = newContent.appendChild(document.createElement('table')); const html = '' + ' | |
---|---|
' + value.key + ' | ' + value.cmd + ' |
' + value.key + ' | ' + value.cmd + ' |