From 988af5f1b8746d7a9522079472788ab63fea9b73 Mon Sep 17 00:00:00 2001 From: Rob Garrison Date: Fri, 25 Aug 2017 18:50:53 -0500 Subject: [PATCH] Store linter rules with sync & LZ compression --- background/storage.js | 21 + edit/lint.js | 21 +- manifest.json | 1 + .../codemirror/addon/lint/css-lint.js | 4 +- .../lz-string/LZString-2xspeedup.js | 512 ++++++++++++++++++ 5 files changed, 547 insertions(+), 12 deletions(-) create mode 100644 vendor-overwrites/lz-string/LZString-2xspeedup.js diff --git a/background/storage.js b/background/storage.js index 9b105357..66a0bfd6 100644 --- a/background/storage.js +++ b/background/storage.js @@ -1,3 +1,4 @@ +/* global LZString */ 'use strict'; const RX_NAMESPACE = new RegExp([/[\s\r\n]*/, @@ -41,6 +42,26 @@ var chromeLocal = { }, }; +// eslint-disable-next-line no-var +var chromeSync = { + get(options) { + return new Promise(resolve => { + chrome.storage.sync.get(options, data => resolve(data)); + }); + }, + set(data) { + return new Promise(resolve => { + chrome.storage.sync.set(data, () => resolve(data)); + }); + }, + getValue(key) { + return chromeSync.get(key).then(data => tryJSONparse(LZString.decompressFromUTF16(data[key]))); + }, + setValue(key, value) { + return chromeSync.set({[key]: LZString.compressToUTF16(JSON.stringify(value))}); + } +}; + function dbExec(method, data) { return new Promise((resolve, reject) => { diff --git a/edit/lint.js b/edit/lint.js index 796a8974..3061bc15 100644 --- a/edit/lint.js +++ b/edit/lint.js @@ -13,23 +13,24 @@ function initLint() { $('#lint h2').addEventListener('click', toggleLintReport); } // initialize storage of rules - BG.chromeLocal.getValue('editorStylelintRules').then(rules => setStylelintRules(rules)); - BG.chromeLocal.getValue('editorCSSLintRules').then(ruleset => setCSSLintRules(ruleset)); + BG.chromeSync.getValue('editorStylelintRules').then(rules => setStylelintRules(rules)); + BG.chromeSync.getValue('editorCSSLintRules').then(ruleset => setCSSLintRules(ruleset)); } -function setStylelintRules(rules = []) { - if (Object.keys(rules).length === 0 && typeof stylelintDefaultConfig !== 'undefined') { +function setStylelintRules(rules) { + // can't use default parameters, because rules may be null + if (Object.keys(rules || []).length === 0 && typeof stylelintDefaultConfig !== 'undefined') { rules = deepCopy(stylelintDefaultConfig.rules); } - BG.chromeLocal.setValue('editorStylelintRules', rules); + BG.chromeSync.setValue('editorStylelintRules', rules); return rules; } -function setCSSLintRules(ruleset = []) { - if (Object.keys(ruleset).length === 0 && typeof csslintDefaultRuleset !== 'undefined') { +function setCSSLintRules(ruleset) { + if (Object.keys(ruleset || []).length === 0 && typeof csslintDefaultRuleset !== 'undefined') { ruleset = Object.assign({}, csslintDefaultRuleset); } - BG.chromeLocal.setValue('editorCSSLintRules', ruleset); + BG.chromeSync.setValue('editorCSSLintRules', ruleset); return ruleset; } @@ -319,12 +320,12 @@ function setupLinterSettingsEvents(popup) { function openStylelintSettings() { const linter = prefs.get('editor.linter'); - BG.chromeLocal.getValue( + BG.chromeSync.getValue( linter === 'stylelint' ? 'editorStylelintRules' : 'editorCSSLintRules' ).then(rules => { - if (rules.length === 0) { + if (!rules || rules.length === 0) { rules = linter === 'stylelint' ? setStylelintRules(rules) : setCSSLintRules(rules); diff --git a/manifest.json b/manifest.json index 4092b411..a374401e 100644 --- a/manifest.json +++ b/manifest.json @@ -21,6 +21,7 @@ "background": { "scripts": [ "js/messaging.js", + "vendor-overwrites/lz-string/LZString-2xspeedup.js", "background/storage.js", "js/prefs.js", "background/background.js", diff --git a/vendor-overwrites/codemirror/addon/lint/css-lint.js b/vendor-overwrites/codemirror/addon/lint/css-lint.js index 0f8195ac..371387c3 100644 --- a/vendor-overwrites/codemirror/addon/lint/css-lint.js +++ b/vendor-overwrites/codemirror/addon/lint/css-lint.js @@ -25,7 +25,7 @@ return found; } /* STYLUS: hack start (part 1) */ - return BG.chromeLocal.getValue('editorCSSLintRules').then((ruleset = csslintDefaultRuleset) => { + return BG.chromeSync.getValue('editorCSSLintRules').then((ruleset = csslintDefaultRuleset) => { // csslintDefaultRuleset stored in csslint-ruleset.js & loaded by edit/lint.js if (Object.keys(ruleset).length === 0) { ruleset = Object.assign({}, csslintDefaultRuleset); @@ -73,7 +73,7 @@ const found = []; window.stylelint = require('stylelint'); if (window.stylelint) { - return BG.chromeLocal.getValue('editorStylelintRules').then((rules = stylelintDefaultConfig.rules) => { + return BG.chromeSync.getValue('editorStylelintRules').then((rules = stylelintDefaultConfig.rules) => { // stylelintDefaultConfig stored in stylelint-config.js & loaded by edit/lint.js if (Object.keys(rules).length === 0) { rules = stylelintDefaultConfig.rules; diff --git a/vendor-overwrites/lz-string/LZString-2xspeedup.js b/vendor-overwrites/lz-string/LZString-2xspeedup.js new file mode 100644 index 00000000..1a3a5a06 --- /dev/null +++ b/vendor-overwrites/lz-string/LZString-2xspeedup.js @@ -0,0 +1,512 @@ +// ==UserScript== +// @name LZString-2xspeedup +// @description 2x speedup via ES6 Map and Set +// @version 1.4.4 +// ==/UserScript== + +// Copyright (c) 2013 Pieroxy +// This work is free. You can redistribute it and/or modify it +// under the terms of the WTFPL, Version 2 +// For more information see LICENSE.txt or http://www.wtfpl.net/ +// +// For more information, the home page: +// http://pieroxy.net/blog/pages/lz-string/testing.html +// +// LZ-based compression algorithm, version 1.4.4 +var LZString = (function() { + +// private property +var f = String.fromCharCode; +var keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; +var keyStrUriSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$"; +var baseReverseDic = {}; + +function getBaseValue(alphabet, character) { + if (!baseReverseDic[alphabet]) { + baseReverseDic[alphabet] = {}; + for (var i=0 ; i>> 8; + buf[i*2+1] = current_value % 256; + } + return buf; + }, + + //decompress from uint8array (UCS-2 big endian format) + decompressFromUint8Array:function (compressed) { + if (compressed===null || compressed===undefined){ + return LZString.decompress(compressed); + } else { + var buf=new Array(compressed.length/2); // 2 bytes per character + for (var i=0, TotalLen=buf.length; i> 1; + } + } else { + value = 1; + for (i=0 ; i> 1; + } + } + context_enlargeIn--; + if (context_enlargeIn == 0) { + context_enlargeIn = Math.pow(2, context_numBits); + context_numBits++; + } + context_dictionaryToCreate.delete(context_w); + } else { + value = context_dictionary.get(context_w); + for (i=0 ; i> 1; + } + + + } + context_enlargeIn--; + if (context_enlargeIn == 0) { + context_enlargeIn = Math.pow(2, context_numBits); + context_numBits++; + } + // Add wc to the dictionary. + context_dictionary.set(context_wc, context_dictSize++); + context_w = String(context_c); + } + } + + // Output the code for w. + if (context_w !== "") { + if (context_dictionaryToCreate.has(context_w)) { + if (context_w.charCodeAt(0)<256) { + for (i=0 ; i> 1; + } + } else { + value = 1; + for (i=0 ; i> 1; + } + } + context_enlargeIn--; + if (context_enlargeIn == 0) { + context_enlargeIn = Math.pow(2, context_numBits); + context_numBits++; + } + context_dictionaryToCreate.delete(context_w); + } else { + value = context_dictionary.get(context_w); + for (i=0 ; i> 1; + } + + + } + context_enlargeIn--; + if (context_enlargeIn == 0) { + context_enlargeIn = Math.pow(2, context_numBits); + context_numBits++; + } + } + + // Mark the end of the stream + value = 2; + for (i=0 ; i> 1; + } + + // Flush the last char + while (true) { + context_data_val = (context_data_val << 1); + if (context_data_position == bitsPerChar-1) { + context_data.push(getCharFromInt(context_data_val)); + break; + } + else context_data_position++; + } + return context_data.join(''); + }, + + decompress: function (compressed) { + if (compressed == null) return ""; + if (compressed == "") return null; + return LZString._decompress(compressed.length, 32768, function(index) { return compressed.charCodeAt(index); }); + }, + + _decompress: function (length, resetValue, getNextValue) { + var dictionary = [], + next, + enlargeIn = 4, + dictSize = 4, + numBits = 3, + entry = "", + result = [], + i, + w, + bits, resb, maxpower, power, + c, + data = {val:getNextValue(0), position:resetValue, index:1}; + + for (i = 0; i < 3; i += 1) { + dictionary[i] = i; + } + + bits = 0; + maxpower = Math.pow(2,2); + power=1; + while (power!=maxpower) { + resb = data.val & data.position; + data.position >>= 1; + if (data.position == 0) { + data.position = resetValue; + data.val = getNextValue(data.index++); + } + bits |= (resb>0 ? 1 : 0) * power; + power <<= 1; + } + + switch (next = bits) { + case 0: + bits = 0; + maxpower = Math.pow(2,8); + power=1; + while (power!=maxpower) { + resb = data.val & data.position; + data.position >>= 1; + if (data.position == 0) { + data.position = resetValue; + data.val = getNextValue(data.index++); + } + bits |= (resb>0 ? 1 : 0) * power; + power <<= 1; + } + c = f(bits); + break; + case 1: + bits = 0; + maxpower = Math.pow(2,16); + power=1; + while (power!=maxpower) { + resb = data.val & data.position; + data.position >>= 1; + if (data.position == 0) { + data.position = resetValue; + data.val = getNextValue(data.index++); + } + bits |= (resb>0 ? 1 : 0) * power; + power <<= 1; + } + c = f(bits); + break; + case 2: + return ""; + } + dictionary[3] = c; + w = c; + result.push(c); + while (true) { + if (data.index > length) { + return ""; + } + + bits = 0; + maxpower = Math.pow(2,numBits); + power=1; + while (power!=maxpower) { + resb = data.val & data.position; + data.position >>= 1; + if (data.position == 0) { + data.position = resetValue; + data.val = getNextValue(data.index++); + } + bits |= (resb>0 ? 1 : 0) * power; + power <<= 1; + } + + switch (c = bits) { + case 0: + bits = 0; + maxpower = Math.pow(2,8); + power=1; + while (power!=maxpower) { + resb = data.val & data.position; + data.position >>= 1; + if (data.position == 0) { + data.position = resetValue; + data.val = getNextValue(data.index++); + } + bits |= (resb>0 ? 1 : 0) * power; + power <<= 1; + } + + dictionary[dictSize++] = f(bits); + c = dictSize-1; + enlargeIn--; + break; + case 1: + bits = 0; + maxpower = Math.pow(2,16); + power=1; + while (power!=maxpower) { + resb = data.val & data.position; + data.position >>= 1; + if (data.position == 0) { + data.position = resetValue; + data.val = getNextValue(data.index++); + } + bits |= (resb>0 ? 1 : 0) * power; + power <<= 1; + } + dictionary[dictSize++] = f(bits); + c = dictSize-1; + enlargeIn--; + break; + case 2: + return result.join(''); + } + + if (enlargeIn == 0) { + enlargeIn = Math.pow(2, numBits); + numBits++; + } + + if (dictionary[c]) { + entry = dictionary[c]; + } else { + if (c === dictSize) { + entry = w + w.charAt(0); + } else { + return null; + } + } + result.push(entry); + + // Add w+entry[0] to the dictionary. + dictionary[dictSize++] = w + entry.charAt(0); + enlargeIn--; + + w = entry; + + if (enlargeIn == 0) { + enlargeIn = Math.pow(2, numBits); + numBits++; + } + + } + } +}; + return LZString; +})(); + +if (typeof define === 'function' && define.amd) { + define(function () { return LZString; }); +} else if( typeof module !== 'undefined' && module != null ) { + module.exports = LZString +} else if( typeof angular !== 'undefined' && angular != null ) { + angular.module('LZString', []) + .factory('LZString', function () { + return LZString; + }); +}