From db1643e9b8955a2267b6cc1473a5aa21360bed36 Mon Sep 17 00:00:00 2001 From: eight Date: Wed, 1 Nov 2017 10:54:14 +0800 Subject: [PATCH] Refactor: pull out colorParser --- js/color-parser.js | 40 +++++++++++++++++++++++++++++++++++++++ js/usercss.js | 42 ++--------------------------------------- manage.html | 2 +- manage/config-dialog.js | 3 +-- manifest.json | 1 + 5 files changed, 45 insertions(+), 43 deletions(-) create mode 100644 js/color-parser.js diff --git a/js/color-parser.js b/js/color-parser.js new file mode 100644 index 00000000..29e7eda8 --- /dev/null +++ b/js/color-parser.js @@ -0,0 +1,40 @@ +'use strict'; + +// eslint-disable-next-line no-var +var colorParser = (() => { + const el = document.createElement('div'); + // https://bugs.webkit.org/show_bug.cgi?id=14563 + document.head.appendChild(el); + + function parseRGB(color) { + const [r, g, b, a = 1] = color.match(/[.\d]+/g).map(Number); + return {r, g, b, a}; + } + + function parse(color) { + el.style.color = color; + if (el.style.color === '') { + throw new Error(chrome.i18n.getMessage('styleMetaErrorColor', color)); + } + color = getComputedStyle(el).color; + el.style.color = ''; + return parseRGB(color); + } + + function format({r, g, b, a = 1}) { + if (a === 1) { + return `rgb(${r}, ${g}, ${b})`; + } + return `rgba(${r}, ${g}, ${b}, ${a})`; + } + + function formatHex({r, g, b, a = null}) { + let hex = '#' + (0x1000000 + (r << 16) + (g << 8) + (b | 0)).toString(16).substr(1); + if (a !== null) { + hex += (0x100 + Math.floor(a * 255)).toString(16).substr(1); + } + return hex; + } + + return {parse, format, formatHex}; +})(); diff --git a/js/usercss.js b/js/usercss.js index 5980c835..fec3180c 100644 --- a/js/usercss.js +++ b/js/usercss.js @@ -1,4 +1,4 @@ -/* global loadScript mozParser semverCompare */ +/* global loadScript mozParser semverCompare colorParser */ 'use strict'; // eslint-disable-next-line no-var @@ -98,44 +98,6 @@ var usercss = (() => { } }; - const colorParser = (() => { - const el = document.createElement('div'); - // https://bugs.webkit.org/show_bug.cgi?id=14563 - document.head.appendChild(el); - - function parseRGB(color) { - const [r, g, b, a = 1] = color.match(/[.\d]+/g).map(Number); - return {r, g, b, a}; - } - - function parse(color) { - el.style.color = color; - if (el.style.color === '') { - throw new Error(chrome.i18n.getMessage('styleMetaErrorColor', color)); - } - color = getComputedStyle(el).color; - el.style.color = ''; - return parseRGB(color); - } - - function format({r, g, b, a = 1}) { - if (a === 1) { - return `rgb(${r}, ${g}, ${b})`; - } - return `rgba(${r}, ${g}, ${b}, ${a})`; - } - - function formatHex({r, g, b, a = null}) { - let hex = '#' + (0x1000000 + (r << 16) + (g << 8) + (b | 0)).toString(16).substr(1); - if (a !== null) { - hex += (0x100 + Math.floor(a * 255)).toString(16).substr(1); - } - return hex; - } - - return {parse, format, formatHex}; - })(); - function getMetaSource(source) { const commentRe = /\/\*[\s\S]*?\*\//g; const metaRe = /==userstyle==[\s\S]*?==\/userstyle==/i; @@ -537,5 +499,5 @@ var usercss = (() => { } } - return {buildMeta, buildCode, colorParser, assignVars}; + return {buildMeta, buildCode, assignVars}; })(); diff --git a/manage.html b/manage.html index 489a3f62..8532a542 100644 --- a/manage.html +++ b/manage.html @@ -150,7 +150,7 @@ - + diff --git a/manage/config-dialog.js b/manage/config-dialog.js index aba5ebe2..1da16812 100644 --- a/manage/config-dialog.js +++ b/manage/config-dialog.js @@ -1,8 +1,7 @@ -/* global usercss messageBox makeLink */ +/* global colorParser messageBox makeLink */ 'use strict'; function configDialog(style) { - const {colorParser} = usercss; const form = buildConfigForm(); return messageBox({ diff --git a/manifest.json b/manifest.json index 4d567ffb..5d2c9eff 100644 --- a/manifest.json +++ b/manifest.json @@ -22,6 +22,7 @@ "scripts": [ "js/messaging.js", "vendor-overwrites/lz-string/LZString-2xspeedup.js", + "js/color-parser.js", "js/usercss.js", "background/storage.js", "background/usercss-helper.js",