Merge pull request #48 from hideheader/preferences
Get and set typed preference values
This commit is contained in:
commit
ecb0c4d8b4
|
@ -316,7 +316,7 @@ chrome.tabs.onAttached.addListener(function(tabId, data) {
|
||||||
if (tabData.url.indexOf(editFullUrl) == 0) {
|
if (tabData.url.indexOf(editFullUrl) == 0) {
|
||||||
chrome.windows.get(tabData.windowId, {populate: true}, function(win) {
|
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
|
// If there's only one tab in this window, it's been dragged to new window
|
||||||
localStorage['openEditInWindow'] = win.tabs.length == 1 ? "true" : "false";
|
prefs.setPref('openEditInWindow', win.tabs.length == 1);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
2
edit.js
2
edit.js
|
@ -24,7 +24,7 @@ function setupCodeMirror(textarea) {
|
||||||
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "CodeMirror-lint-markers"],
|
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "CodeMirror-lint-markers"],
|
||||||
matchBrackets: true,
|
matchBrackets: true,
|
||||||
lint: CodeMirror.lint.css,
|
lint: CodeMirror.lint.css,
|
||||||
smartIndent: (typeof localStorage["smart-indent"] == "undefined") || localStorage["smart-indent"] == "true",
|
smartIndent: prefs.getPref("smart-indent"),
|
||||||
extraKeys: {"Ctrl-Space": "autocomplete"}
|
extraKeys: {"Ctrl-Space": "autocomplete"}
|
||||||
});
|
});
|
||||||
editors.push(cm);
|
editors.push(cm);
|
||||||
|
|
|
@ -10,7 +10,7 @@ function notifyAllTabs(request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateBadgeText(tab) {
|
function updateBadgeText(tab) {
|
||||||
if (localStorage["show-badge"] == "true") {
|
if (prefs.getPref("show-badge")) {
|
||||||
function stylesReceived(styles) {
|
function stylesReceived(styles) {
|
||||||
var t = getBadgeText(styles);
|
var t = getBadgeText(styles);
|
||||||
console.log("Tab " + tab.id + " (" + tab.url + ") badge text set to '" + t + "'.");
|
console.log("Tab " + tab.id + " (" + tab.url + ") badge text set to '" + t + "'.");
|
||||||
|
|
2
popup.js
2
popup.js
|
@ -143,7 +143,7 @@ function getId(event) {
|
||||||
|
|
||||||
function openLinkInTabOrWindow(event) {
|
function openLinkInTabOrWindow(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
if (localStorage['openEditInWindow'] == 'true') {
|
if (prefs.getPref('openEditInWindow', false)) {
|
||||||
chrome.windows.create({url: event.target.href});
|
chrome.windows.create({url: event.target.href});
|
||||||
} else {
|
} else {
|
||||||
chrome.tabs.create({url: event.target.href});
|
chrome.tabs.create({url: event.target.href});
|
||||||
|
|
70
storage.js
70
storage.js
|
@ -21,20 +21,10 @@ function getDatabase(ready, error) {
|
||||||
} else if (stylishDb.version == "1.4") {
|
} else if (stylishDb.version == "1.4") {
|
||||||
dbV15(stylishDb, error, ready);
|
dbV15(stylishDb, error, ready);
|
||||||
} else {
|
} else {
|
||||||
defaultPrefs();
|
|
||||||
ready(stylishDb);
|
ready(stylishDb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function defaultPrefs() {
|
|
||||||
if (!("show-badge" in localStorage)) {
|
|
||||||
localStorage["show-badge"] = true;
|
|
||||||
}
|
|
||||||
if (!("smart-indent" in localStorage)) {
|
|
||||||
localStorage["smart-indent"] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function dbV11(d, error, done) {
|
function dbV11(d, error, done) {
|
||||||
d.changeVersion(d.version, '1.1', function (t) {
|
d.changeVersion(d.version, '1.1', function (t) {
|
||||||
t.executeSql('CREATE TABLE styles (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, url TEXT, updateUrl TEXT, md5Url TEXT, name TEXT NOT NULL, code TEXT NOT NULL, enabled INTEGER NOT NULL, originalCode TEXT NULL);');
|
t.executeSql('CREATE TABLE styles (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, url TEXT, updateUrl TEXT, md5Url TEXT, name TEXT NOT NULL, code TEXT NOT NULL, enabled INTEGER NOT NULL, originalCode TEXT NULL);');
|
||||||
|
@ -148,23 +138,69 @@ function isCheckbox(el) {
|
||||||
|
|
||||||
function changePref(event) {
|
function changePref(event) {
|
||||||
var el = event.target;
|
var el = event.target;
|
||||||
var value = isCheckbox(el) ? el.checked : el.value;
|
prefs.setPref(el.id, isCheckbox(el) ? el.checked : el.value);
|
||||||
localStorage[el.id] = value
|
|
||||||
notifyAllTabs({method: "prefChanged", prefName: el.id, value: value});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accepts a hash of pref name to default value
|
// Accepts a hash of pref name to default value
|
||||||
function loadPrefs(prefs) {
|
function loadPrefs(prefs) {
|
||||||
for (var id in prefs) {
|
for (var id in prefs) {
|
||||||
var value = typeof localStorage[id] == "undefined" ? prefs[id] : localStorage[id];
|
var value = this.prefs.getPref(id);
|
||||||
var el = document.getElementById(id);
|
var el = document.getElementById(id);
|
||||||
if (isCheckbox(el)) {
|
if (isCheckbox(el)) {
|
||||||
if (value == "true") {
|
el.checked = value;
|
||||||
el.checked = true;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
el.value = value;
|
el.value = value;
|
||||||
}
|
}
|
||||||
el.addEventListener("change", changePref);
|
el.addEventListener("change", changePref);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var prefs = {
|
||||||
|
// NB: localStorage["not_key"] is undefined, localStorage.getItem("not_key") is null
|
||||||
|
|
||||||
|
// defaults
|
||||||
|
"openEditInWindow": false, // new editor opens in a own browser window
|
||||||
|
"show-badge": true, // display text on popup menu icon
|
||||||
|
"smart-indent": true, // CodeMirror smart indent
|
||||||
|
|
||||||
|
"popup.breadcrumbs": true, // display "New style" links as URL breadcrumbs
|
||||||
|
"popup.breadcrumbs.usePath": false, // use URL path for "this URL"
|
||||||
|
|
||||||
|
"popup.enabledFirst": true, // display enabled styles before disabled styles
|
||||||
|
"manage.enabledFirst": true, // display enabled styles before disabled styles
|
||||||
|
|
||||||
|
"observer.observeFrameContent": false, // [hh] add MutationObserver inside IFRAMEs
|
||||||
|
"observer.observeFrameLoad": false, // [hh] add onLoad listener to IFRAMEs
|
||||||
|
// https://github.com/JasonBarnabe/stylish-chrome/pull/39#issuecomment-76681235
|
||||||
|
|
||||||
|
NO_DEFAULT_PREFERENCE: "No default preference for '%s'",
|
||||||
|
UNHANDLED_DATA_TYPE: "Default '%s' is of type '%s' - what should be done with it?",
|
||||||
|
|
||||||
|
getPref: function(key, ifUndefined) {
|
||||||
|
// Returns localStorage[key], ifUndefined, this[key], or undefined
|
||||||
|
// as type of ifUndefined, this[key], or localStorage[key]
|
||||||
|
if (ifUndefined === undefined) ifUndefined = this[key]; // default value
|
||||||
|
var value = localStorage[key];
|
||||||
|
if (undefined === value) { // no user preference
|
||||||
|
if (ifUndefined === undefined) console.error(this.NO_DEFAULT_PREFERENCE, key);
|
||||||
|
return ifUndefined;
|
||||||
|
}
|
||||||
|
switch (typeof ifUndefined) {
|
||||||
|
case "boolean": return value.toLowerCase() === "true";
|
||||||
|
case "number": return Number(value);
|
||||||
|
case "object": return JSON.parse(value);
|
||||||
|
case "string": break;
|
||||||
|
case "undefined": console.warn(this.NO_DEFAULT_PREFERENCE, key); break;
|
||||||
|
default: console.error(UNHANDLED_DATA_TYPE, key, typeof ifUndefined);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
},
|
||||||
|
setPref: function(key, value) {
|
||||||
|
if (!(key in this)) console.warn(this.NO_DEFAULT_PREFERENCE, key);
|
||||||
|
if (value === undefined) localStorage.removeItem(key);
|
||||||
|
else localStorage.setItem(key, JSON.stringify(value));
|
||||||
|
|
||||||
|
notifyAllTabs({method: "prefChanged", prefName: key, value: value});
|
||||||
|
},
|
||||||
|
removePref: function(key) { setPref(key, undefined) }
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user