From d971bbda8a39129ecbf4d1d337bd45ef9c1a5891 Mon Sep 17 00:00:00 2001 From: tophf Date: Mon, 5 Oct 2015 14:27:17 +0300 Subject: [PATCH] Remove unneeded "Pref" word from `prefs` methods --- background.js | 16 ++++++++-------- edit.js | 40 ++++++++++++++++++++-------------------- manage.js | 4 ++-- messaging.js | 4 ++-- popup.js | 18 +++++++++--------- storage.js | 25 +++++++++++++------------ 6 files changed, 54 insertions(+), 53 deletions(-) diff --git a/background.js b/background.js index 27218ef6..e10f464c 100644 --- a/background.js +++ b/background.js @@ -71,7 +71,7 @@ chrome.commands.onCommand.addListener(function(command) { break; case "styleDisableAll": disableAllStylesToggle(); - chrome.contextMenus.update("disableAll", {checked: prefs.getPref("disableAll")}); + chrome.contextMenus.update("disableAll", {checked: prefs.get("disableAll")}); break; } }); @@ -81,11 +81,11 @@ chrome.commands.onCommand.addListener(function(command) { runTryCatch(function() { chrome.contextMenus.create({ id: "show-badge", title: chrome.i18n.getMessage("menuShowBadge"), - type: "checkbox", contexts: ["browser_action"], checked: prefs.getPref("show-badge") + type: "checkbox", contexts: ["browser_action"], checked: prefs.get("show-badge") }, function() { var clearError = chrome.runtime.lastError }); chrome.contextMenus.create({ id: "disableAll", title: chrome.i18n.getMessage("disableAllStyles"), - type: "checkbox", contexts: ["browser_action"], checked: prefs.getPref("disableAll") + type: "checkbox", contexts: ["browser_action"], checked: prefs.get("disableAll") }, function() { var clearError = chrome.runtime.lastError }); }); @@ -93,15 +93,15 @@ chrome.contextMenus.onClicked.addListener(function(info, tab) { if (info.menuItemId == "disableAll") { disableAllStylesToggle(info.checked); } else { - prefs.setPref(info.menuItemId, info.checked); + prefs.set(info.menuItemId, info.checked); } }); function disableAllStylesToggle(newState) { if (newState === undefined || newState === null) { - newState = !prefs.getPref("disableAll"); + newState = !prefs.get("disableAll"); } - prefs.setPref("disableAll", newState); + prefs.set("disableAll", newState); } function getStyles(options, callback) { @@ -114,7 +114,7 @@ function getStyles(options, callback) { var asHash = "asHash" in options ? options.asHash : false; var callCallback = function() { - var styles = asHash ? {disableAll: prefs.getPref("disableAll", false)} : []; + var styles = asHash ? {disableAll: prefs.get("disableAll", false)} : []; cachedStyles.forEach(function(style) { if (enabled != null && fixBoolean(style.enabled) != enabled) { return; @@ -387,7 +387,7 @@ chrome.tabs.onAttached.addListener(function(tabId, data) { if (tabData.url.indexOf(editFullUrl) == 0) { chrome.windows.get(tabData.windowId, {populate: true}, function(win) { // If there's only one tab in this window, it's been dragged to new window - prefs.setPref('openEditInWindow', win.tabs.length == 1); + prefs.set("openEditInWindow", win.tabs.length == 1); }); } }); diff --git a/edit.js b/edit.js index 74f0a82b..552157cc 100644 --- a/edit.js +++ b/edit.js @@ -132,16 +132,16 @@ function initCodeMirror() { foldGutter: true, gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "CodeMirror-lint-markers"], matchBrackets: true, - lint: {getAnnotations: CodeMirror.lint.css, delay: prefs.getPref("editor.lintDelay")}, - lintReportDelay: prefs.getPref("editor.lintReportDelay"), + lint: {getAnnotations: CodeMirror.lint.css, delay: prefs.get("editor.lintDelay")}, + lintReportDelay: prefs.get("editor.lintReportDelay"), styleActiveLine: true, theme: "default", - keyMap: prefs.getPref("editor.keyMap"), + keyMap: prefs.get("editor.keyMap"), extraKeys: { // independent of current keyMap "Alt-PageDown": "nextEditor", "Alt-PageUp": "prevEditor" } - }, prefs.getPref("editor.options")); + }, prefs.get("editor.options")); // additional commands CM.commands.jumpToLine = jumpToLine; @@ -219,8 +219,8 @@ function initCodeMirror() { }); } - // preload the theme so that CodeMirror can calculate its metrics in DOMContentLoaded->loadPrefs() - var theme = prefs.getPref("editor.theme"); + // preload the theme so that CodeMirror can calculate its metrics in DOMContentLoaded->setupLivePrefs() + var theme = prefs.get("editor.theme"); document.getElementById("cm-theme").href = theme == "default" ? "" : "codemirror/theme/" + theme + ".css"; // initialize global editor controls @@ -242,7 +242,7 @@ function initCodeMirror() { } document.getElementById("editor.keyMap").innerHTML = optionsHtmlFromArray(Object.keys(CM.keyMap).sort()); document.getElementById("options").addEventListener("change", acmeEventListener, false); - loadPrefs( + setupLivePrefs( document.querySelectorAll("#options *[data-option][id^='editor.']") .map(function(option) { return option.id }) ); @@ -270,8 +270,8 @@ function acmeEventListener(event) { // use non-localized "default" internally if (!value || value == "default" || value == t("defaultTheme")) { value = "default"; - if (prefs.getPref(el.id) != value) { - prefs.setPref(el.id, value); + if (prefs.get(el.id) != value) { + prefs.set(el.id, value); } themeLink.href = ""; el.selectedIndex = 0; @@ -386,7 +386,7 @@ document.addEventListener("wheel", function(event) { chrome.tabs.query({currentWindow: true}, function(tabs) { var windowId = tabs[0].windowId; - if (prefs.getPref("openEditInWindow")) { + if (prefs.get("openEditInWindow")) { if (tabs.length == 1 && window.history.length == 1) { chrome.windows.getAll(function(windows) { if (windows.length > 1) { @@ -420,7 +420,7 @@ function goBackToManage(event) { window.onbeforeunload = function() { if (saveSizeOnClose) { - prefs.setPref("windowPosition", { + prefs.set("windowPosition", { left: screenLeft, top: screenTop, width: outerWidth, @@ -964,9 +964,9 @@ function beautify(event) { script.onload = doBeautify; } function doBeautify() { - var tabs = prefs.getPref("editor.indentWithTabs"); - var options = prefs.getPref("editor.beautify"); - options.indent_size = tabs ? 1 : prefs.getPref("editor.tabSize"); + var tabs = prefs.get("editor.indentWithTabs"); + var options = prefs.get("editor.beautify"); + options.indent_size = tabs ? 1 : prefs.get("editor.tabSize"); options.indent_char = tabs ? "\t" : " "; var section = getSectionForChild(event.target); @@ -1015,7 +1015,7 @@ function beautify(event) { document.querySelector(".beautify-options").addEventListener("change", function(event) { var value = event.target.selectedIndex > 0; options[event.target.dataset.option] = value; - prefs.setPref("editor.beautify", options); + prefs.set("editor.beautify", options); event.target.parentNode.setAttribute("newline", value.toString()); doBeautify(); }); @@ -1090,7 +1090,7 @@ function initWithStyle(style) { function add() { var sectionDiv = addSection(null, queue.shift()); maximizeCodeHeight(sectionDiv, !queue.length); - updateLintReport(getCodeMirrorForSection(sectionDiv), prefs.getPref("editor.lintDelay")); + updateLintReport(getCodeMirrorForSection(sectionDiv), prefs.get("editor.lintDelay")); } } @@ -1442,12 +1442,12 @@ function showToMozillaHelp() { } function showKeyMapHelp() { - var keyMap = mergeKeyMaps({}, prefs.getPref("editor.keyMap"), CodeMirror.defaults.extraKeys); + var keyMap = mergeKeyMaps({}, prefs.get("editor.keyMap"), CodeMirror.defaults.extraKeys); var keyMapSorted = Object.keys(keyMap) .map(function(key) { return {key: key, cmd: keyMap[key]} }) .concat([{key: "Shift-Ctrl-Wheel", cmd: "scrollWindow"}]) .sort(function(a, b) { return a.cmd < b.cmd || (a.cmd == b.cmd && a.key < b.key) ? -1 : 1 }); - showHelp(t("cm_keyMap") + ": " + prefs.getPref("editor.keyMap"), + showHelp(t("cm_keyMap") + ": " + prefs.get("editor.keyMap"), '' + '' + '' + @@ -1564,8 +1564,8 @@ function showCodeMirrorPopup(title, html, options) { matchBrackets: true, lint: {getAnnotations: CodeMirror.lint.css, delay: 0}, styleActiveLine: true, - theme: prefs.getPref("editor.theme"), - keyMap: prefs.getPref("editor.keyMap") + theme: prefs.get("editor.theme"), + keyMap: prefs.get("editor.keyMap") }, options)); popup.codebox.focus(); popup.codebox.on("focus", function() { hotkeyRerouter.setState(false) }); diff --git a/manage.js b/manage.js index 5ab077d1..fa9d9dcf 100644 --- a/manage.js +++ b/manage.js @@ -110,7 +110,7 @@ function createStyleElement(style) { event.stopPropagation(); if (openWindow || openBackgroundTab || openForegroundTab) { if (openWindow) { - var options = prefs.getPref("windowPosition"); + var options = prefs.get("windowPosition"); options.url = url; chrome.windows.create(options); } else { @@ -475,7 +475,7 @@ document.addEventListener("DOMContentLoaded", function() { document.getElementById("search").addEventListener("input", searchStyles); searchStyles(true); // re-apply filtering on history Back - loadPrefs([ + setupLivePrefs([ "manage.onlyEnabled", "manage.onlyEdited", "show-badge", diff --git a/messaging.js b/messaging.js index 5ecadfff..1d2c9eb6 100644 --- a/messaging.js +++ b/messaging.js @@ -44,13 +44,13 @@ function updateIcon(tab, styles) { }); function stylesReceived(styles) { - var disableAll = "disableAll" in styles ? styles.disableAll : prefs.getPref("disableAll"); + var disableAll = "disableAll" in styles ? styles.disableAll : prefs.get("disableAll"); var postfix = styles.length == 0 || disableAll ? "w" : ""; chrome.browserAction.setIcon({ path: {19: "19" + postfix + ".png", 38: "38" + postfix + ".png"}, tabId: tab.id }); - var t = prefs.getPref("show-badge") && styles.length ? ("" + styles.length) : ""; + var t = prefs.get("show-badge") && styles.length ? ("" + styles.length) : ""; chrome.browserAction.setBadgeText({text: t, tabId: tab.id}); chrome.browserAction.setBadgeBackgroundColor({color: disableAll ? "#aaa" : [0, 0, 0, 0]}); //console.log("Tab " + tab.id + " (" + tab.url + ") badge text set to '" + t + "'."); diff --git a/popup.js b/popup.js index 83ce5d1d..6cf7648f 100644 --- a/popup.js +++ b/popup.js @@ -3,7 +3,7 @@ writeStyleTemplate.className = "write-style-link"; var installed = document.getElementById("installed"); -if (!prefs.getPref("popup.stylesFirst")) { +if (!prefs.get("popup.stylesFirst")) { document.body.insertBefore(document.querySelector("body > .actions"), installed); } @@ -29,14 +29,14 @@ function updatePopUp(url) { var urlLink = writeStyleTemplate.cloneNode(true); urlLink.href = "edit.html?url-prefix=" + encodeURIComponent(url); urlLink.appendChild(document.createTextNode( // switchable; default="this URL" - !prefs.getPref("popup.breadcrumbs.usePath") + !prefs.get("popup.breadcrumbs.usePath") ? t("writeStyleForURL").replace(/ /g, "\u00a0") : /\/\/[^/]+\/(.*)/.exec(url)[1] )); urlLink.title = "url-prefix(\"$\")".replace("$", url); writeStyleLinks.push(urlLink); document.querySelector("#write-style").appendChild(urlLink) - if (prefs.getPref("popup.breadcrumbs")) { // switchable; default=enabled + if (prefs.get("popup.breadcrumbs")) { // switchable; default=enabled urlLink.addEventListener("mouseenter", function(event) { this.parentNode.classList.add("url()") }, false); urlLink.addEventListener("focus", function(event) { this.parentNode.classList.add("url()") }, false); urlLink.addEventListener("mouseleave", function(event) { this.parentNode.classList.remove("url()") }, false); @@ -63,7 +63,7 @@ function updatePopUp(url) { link.addEventListener("click", openLinkInTabOrWindow, false); container.appendChild(link); }); - if (prefs.getPref("popup.breadcrumbs")) { + if (prefs.get("popup.breadcrumbs")) { container.classList.add("breadcrumbs"); container.appendChild(container.removeChild(container.firstChild)); } @@ -71,7 +71,7 @@ function updatePopUp(url) { } function showStyles(styles) { - var enabledFirst = prefs.getPref("popup.enabledFirst"); + var enabledFirst = prefs.get("popup.enabledFirst"); styles.sort(function(a, b) { if (enabledFirst && a.enabled !== b.enabled) return !(a.enabled < b.enabled) ? -1 : 1; return a.name.localeCompare(b.name); @@ -146,9 +146,9 @@ function getId(event) { function openLinkInTabOrWindow(event) { event.preventDefault(); - if (prefs.getPref('openEditInWindow', false)) { + if (prefs.get("openEditInWindow", false)) { var options = {url: event.target.href} - var wp = prefs.getPref('windowPosition', {}); + var wp = prefs.get("windowPosition", {}); for (var k in wp) options[k] = wp[k]; chrome.windows.create(options); } else { @@ -204,6 +204,6 @@ chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { }); document.getElementById("disableAll").addEventListener("change", function(event) { - installed.classList.toggle("disabled", prefs.getPref("disableAll")); + installed.classList.toggle("disabled", prefs.get("disableAll")); }); -loadPrefs(["disableAll"]); +setupLivePrefs(["disableAll"]); diff --git a/storage.js b/storage.js index ba0d6cc5..621e5395 100644 --- a/storage.js +++ b/storage.js @@ -136,13 +136,14 @@ function isCheckbox(el) { return el.nodeName.toLowerCase() == "input" && "checkbox" == el.type.toLowerCase(); } -// Accepts an array of pref names (values are fetched via prefs.getPref) -function loadPrefs(IDs) { +// Accepts an array of pref names (values are fetched via prefs.get) +// and establishes a two-way connection between the document elements and the actual prefs +function setupLivePrefs(IDs) { var localIDs = {}; IDs.forEach(function(id) { localIDs[id] = true; updateElement(id).addEventListener("change", function() { - prefs.setPref(this.id, isCheckbox(this) ? this.checked : this.value); + prefs.set(this.id, isCheckbox(this) ? this.checked : this.value); }); }); chrome.extension.onMessage.addListener(function(request) { @@ -152,7 +153,7 @@ function loadPrefs(IDs) { }); function updateElement(id) { var el = document.getElementById(id); - el[isCheckbox(el) ? "checked" : "value"] = prefs.getPref(id); + el[isCheckbox(el) ? "checked" : "value"] = prefs.get(id); el.dispatchEvent(new Event("change", {bubbles: true, cancelable: true})); return el; } @@ -200,7 +201,7 @@ var prefs = chrome.extension.getBackgroundPage().prefs || new function Prefs() { Object.defineProperty(this, "readOnlyValues", {value: {}}); - Prefs.prototype.getPref = function(key, defaultValue) { + Prefs.prototype.get = function(key, defaultValue) { if (key in values) { return values[key]; } @@ -213,11 +214,11 @@ var prefs = chrome.extension.getBackgroundPage().prefs || new function Prefs() { console.warn("No default preference for '%s'", key); }; - Prefs.prototype.getAllPrefs = function(key) { + Prefs.prototype.getAll = function(key) { return deepCopy(values); }; - Prefs.prototype.setPref = function(key, value, options) { + Prefs.prototype.set = function(key, value, options) { var oldValue = deepCopy(values[key]); values[key] = value; defineReadonlyProperty(this.readOnlyValues, key, value); @@ -226,7 +227,7 @@ var prefs = chrome.extension.getBackgroundPage().prefs || new function Prefs() { } }; - Prefs.prototype.removePref = function(key) { me.setPref(key, undefined) }; + Prefs.prototype.remove = function(key) { me.set(key, undefined) }; Prefs.prototype.broadcast = function(key, value, options) { var message = {method: "prefChanged", prefName: key, value: value}; @@ -244,18 +245,18 @@ var prefs = chrome.extension.getBackgroundPage().prefs || new function Prefs() { }; Object.keys(defaults).forEach(function(key) { - me.setPref(key, defaults[key], {noBroadcast: true}); + me.set(key, defaults[key], {noBroadcast: true}); }); chrome.storage.sync.get("settings", function(result) { var synced = result.settings; for (var key in defaults) { if (synced && (key in synced)) { - me.setPref(key, synced[key], {noSync: true}); + me.set(key, synced[key], {noSync: true}); } else { var value = tryMigrating(key); if (value !== undefined) { - me.setPref(key, value); + me.set(key, value); } } } @@ -267,7 +268,7 @@ var prefs = chrome.extension.getBackgroundPage().prefs || new function Prefs() { if (synced) { for (key in defaults) { if (key in synced) { - me.setPref(key, synced[key], {noSync: true}); + me.set(key, synced[key], {noSync: true}); } } } else {