Convert to Unix linebreaks

This commit is contained in:
Jason Barnabe 2015-01-29 12:41:45 -06:00
parent db18784f84
commit c4c92c6c27
2 changed files with 340 additions and 340 deletions

View File

@ -1,299 +1,299 @@
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
switch (request.method) { switch (request.method) {
case "getStyles": case "getStyles":
getStyles(request, function(r) { getStyles(request, function(r) {
sendResponse(r); sendResponse(r);
if (localStorage["show-badge"] == "true") { if (localStorage["show-badge"] == "true") {
if (request.updateBadge) { if (request.updateBadge) {
var t = getBadgeText(r); var t = getBadgeText(r);
console.log("Tab " + sender.tab.id + " (" + sender.tab.url + ") badge text set to '" + t + "'."); console.log("Tab " + sender.tab.id + " (" + sender.tab.url + ") badge text set to '" + t + "'.");
chrome.browserAction.setBadgeText({text: t, tabId: sender.tab.id}); chrome.browserAction.setBadgeText({text: t, tabId: sender.tab.id});
} else { } else {
console.log("Tab " + sender.tab.id + " (" + sender.tab.url + ") doesn't get badge text."); console.log("Tab " + sender.tab.id + " (" + sender.tab.url + ") doesn't get badge text.");
} }
} }
}); });
return true; return true;
case "getStyleApplies": case "getStyleApplies":
sendResponse(getApplicableSections(request.style, request.url)); sendResponse(getApplicableSections(request.style, request.url));
return true; return true;
case "saveStyle": case "saveStyle":
saveStyle(request, sendResponse); saveStyle(request, sendResponse);
return true; return true;
case "styleChanged": case "styleChanged":
cachedStyles = null; cachedStyles = null;
break; break;
case "healthCheck": case "healthCheck":
getDatabase(function() { sendResponse(true); }, function() { sendResponse(false); }); getDatabase(function() { sendResponse(true); }, function() { sendResponse(false); });
break; break;
} }
}); });
function getStyles(options, callback) { function getStyles(options, callback) {
var enabled = fixBoolean(options.enabled); var enabled = fixBoolean(options.enabled);
var url = "url" in options ? options.url : null; var url = "url" in options ? options.url : null;
var id = "id" in options ? options.id : null; var id = "id" in options ? options.id : null;
var matchUrl = "matchUrl" in options ? options.matchUrl : null; var matchUrl = "matchUrl" in options ? options.matchUrl : null;
var callCallback = function() { var callCallback = function() {
callback(cachedStyles.filter(function(style) { callback(cachedStyles.filter(function(style) {
if (enabled != null && fixBoolean(style.enabled) != enabled) { if (enabled != null && fixBoolean(style.enabled) != enabled) {
return false; return false;
} }
if (url != null && style.url != url) { if (url != null && style.url != url) {
return false; return false;
} }
if (id != null && style.id != id) { if (id != null && style.id != id) {
return false; return false;
} }
if (matchUrl != null && getApplicableSections(style, matchUrl) == 0) { if (matchUrl != null && getApplicableSections(style, matchUrl) == 0) {
return false; return false;
} }
return true; return true;
})); }));
} }
if (cachedStyles) { if (cachedStyles) {
callCallback(); callCallback();
return; return;
} }
getDatabase(function(db) { getDatabase(function(db) {
db.readTransaction(function (t) { db.readTransaction(function (t) {
var where = ""; var where = "";
var params = []; var params = [];
t.executeSql('SELECT DISTINCT s.*, se.id section_id, se.code, sm.name metaName, sm.value metaValue FROM styles s LEFT JOIN sections se ON se.style_id = s.id LEFT JOIN section_meta sm ON sm.section_id = se.id WHERE 1' + where + ' ORDER BY s.id, se.id, sm.id', params, function (t, r) { t.executeSql('SELECT DISTINCT s.*, se.id section_id, se.code, sm.name metaName, sm.value metaValue FROM styles s LEFT JOIN sections se ON se.style_id = s.id LEFT JOIN section_meta sm ON sm.section_id = se.id WHERE 1' + where + ' ORDER BY s.id, se.id, sm.id', params, function (t, r) {
cachedStyles = []; cachedStyles = [];
var currentStyle = null; var currentStyle = null;
var currentSection = null; var currentSection = null;
for (var i = 0; i < r.rows.length; i++) { for (var i = 0; i < r.rows.length; i++) {
var values = r.rows.item(i); var values = r.rows.item(i);
var metaName = null; var metaName = null;
switch (values.metaName) { switch (values.metaName) {
case null: case null:
break; break;
case "url": case "url":
metaName = "urls"; metaName = "urls";
break; break;
case "url-prefix": case "url-prefix":
metaName = "urlPrefixes"; metaName = "urlPrefixes";
break; break;
case "domain": case "domain":
var metaName = "domains"; var metaName = "domains";
break; break;
case "regexps": case "regexps":
var metaName = "regexps"; var metaName = "regexps";
break; break;
default: default:
var metaName = values.metaName + "s"; var metaName = values.metaName + "s";
} }
var metaValue = values.metaValue; var metaValue = values.metaValue;
if (currentStyle == null || currentStyle.id != values.id) { if (currentStyle == null || currentStyle.id != values.id) {
currentStyle = {id: values.id, url: values.url, updateUrl: values.updateUrl, md5Url: values.md5Url, name: values.name, enabled: values.enabled, originalMd5: values.originalMd5, sections: []}; currentStyle = {id: values.id, url: values.url, updateUrl: values.updateUrl, md5Url: values.md5Url, name: values.name, enabled: values.enabled, originalMd5: values.originalMd5, sections: []};
cachedStyles.push(currentStyle); cachedStyles.push(currentStyle);
} }
if (currentSection == null || currentSection.id != values.section_id) { if (currentSection == null || currentSection.id != values.section_id) {
currentSection = {id: values.section_id, code: values.code}; currentSection = {id: values.section_id, code: values.code};
currentStyle.sections.push(currentSection); currentStyle.sections.push(currentSection);
} }
if (metaName && metaValue) { if (metaName && metaValue) {
if (currentSection[metaName]) { if (currentSection[metaName]) {
currentSection[metaName].push(metaValue); currentSection[metaName].push(metaValue);
} else { } else {
currentSection[metaName] = [metaValue]; currentSection[metaName] = [metaValue];
} }
} }
} }
callCallback(); callCallback();
}, reportError); }, reportError);
}, reportError); }, reportError);
}, reportError); }, reportError);
} }
function fixBoolean(b) { function fixBoolean(b) {
if (typeof b != "undefined") { if (typeof b != "undefined") {
return b != "false"; return b != "false";
} }
return null; return null;
} }
const namespacePattern = /^\s*@namespace\s+([a-zA-Z]+\s+)?url\(\"?http:\/\/www.w3.org\/1999\/xhtml\"?\);?\s*$/; const namespacePattern = /^\s*@namespace\s+([a-zA-Z]+\s+)?url\(\"?http:\/\/www.w3.org\/1999\/xhtml\"?\);?\s*$/;
function getApplicableSections(style, url) { function getApplicableSections(style, url) {
var sections = style.sections.filter(function(section) { var sections = style.sections.filter(function(section) {
return sectionAppliesToUrl(section, url); return sectionAppliesToUrl(section, url);
}); });
// ignore if it's just a namespace // ignore if it's just a namespace
if (sections.length == 1 && namespacePattern.test(sections[0].code)) { if (sections.length == 1 && namespacePattern.test(sections[0].code)) {
return []; return [];
} }
return sections; return sections;
} }
function sectionAppliesToUrl(section, url) { function sectionAppliesToUrl(section, url) {
// only http and https allowed // only http and https allowed
if (url.indexOf("http") != 0) { if (url.indexOf("http") != 0) {
return false; return false;
} }
if (!section.urls && !section.domains && !section.urlPrefixes && !section.regexps) { if (!section.urls && !section.domains && !section.urlPrefixes && !section.regexps) {
console.log(section.id + " is global"); console.log(section.id + " is global");
return true; return true;
} }
if (section.urls && section.urls.indexOf(url) != -1) { if (section.urls && section.urls.indexOf(url) != -1) {
console.log(section.id + " applies to " + url + " due to URL rules"); console.log(section.id + " applies to " + url + " due to URL rules");
return true; return true;
} }
if (section.urlPrefixes && section.urlPrefixes.some(function(prefix) { if (section.urlPrefixes && section.urlPrefixes.some(function(prefix) {
return url.indexOf(prefix) == 0; return url.indexOf(prefix) == 0;
})) { })) {
console.log(section.id + " applies to " + url + " due to URL prefix rules"); console.log(section.id + " applies to " + url + " due to URL prefix rules");
return true; return true;
} }
if (section.domains && getDomains(url).some(function(domain) { if (section.domains && getDomains(url).some(function(domain) {
return section.domains.indexOf(domain) != -1; return section.domains.indexOf(domain) != -1;
})) { })) {
console.log(section.id + " applies due to " + url + " due to domain rules"); console.log(section.id + " applies due to " + url + " due to domain rules");
return true; return true;
} }
if (section.regexps && section.regexps.some(function(regexp) { if (section.regexps && section.regexps.some(function(regexp) {
// we want to match the full url, so add ^ and $ if not already present // we want to match the full url, so add ^ and $ if not already present
if (regexp[0] != "^") { if (regexp[0] != "^") {
regexp = "^" + regexp; regexp = "^" + regexp;
} }
if (regexp[regexp.length - 1] != "$") { if (regexp[regexp.length - 1] != "$") {
regexp += "$"; regexp += "$";
} }
try { try {
var re = new RegExp(regexp); var re = new RegExp(regexp);
} catch (ex) { } catch (ex) {
console.log(section.id + "'s regexp '" + regexp + "' is not valid"); console.log(section.id + "'s regexp '" + regexp + "' is not valid");
return false; return false;
} }
return (re).test(url); return (re).test(url);
})) { })) {
console.log(section.id + " applies to " + url + " due to regexp rules"); console.log(section.id + " applies to " + url + " due to regexp rules");
return true; return true;
} }
console.log(section.id + " does not apply due to " + url); console.log(section.id + " does not apply due to " + url);
return false; return false;
} }
var cachedStyles = null; var cachedStyles = null;
function saveStyle(o, callback) { function saveStyle(o, callback) {
getDatabase(function(db) { getDatabase(function(db) {
db.transaction(function(t) { db.transaction(function(t) {
if (o.id) { if (o.id) {
// update whatever's been passed // update whatever's been passed
if ("name" in o) { if ("name" in o) {
t.executeSql('UPDATE styles SET name = ? WHERE id = ?;', [o.name, o.id]); t.executeSql('UPDATE styles SET name = ? WHERE id = ?;', [o.name, o.id]);
} }
if ("enabled" in o) { if ("enabled" in o) {
t.executeSql('UPDATE styles SET enabled = ? WHERE id = ?;', [o.enabled, o.id]); t.executeSql('UPDATE styles SET enabled = ? WHERE id = ?;', [o.enabled, o.id]);
} }
if ("url" in o) { if ("url" in o) {
t.executeSql('UPDATE styles SET url = ? WHERE id = ?;', [o.url, o.id]); t.executeSql('UPDATE styles SET url = ? WHERE id = ?;', [o.url, o.id]);
} }
if ("updateUrl" in o) { if ("updateUrl" in o) {
t.executeSql('UPDATE styles SET updateUrl = ? WHERE id = ?;', [o.updateUrl, o.id]); t.executeSql('UPDATE styles SET updateUrl = ? WHERE id = ?;', [o.updateUrl, o.id]);
} }
if ("md5Url" in o) { if ("md5Url" in o) {
t.executeSql('UPDATE styles SET md5Url = ? WHERE id = ?;', [o.md5Url, o.id]); t.executeSql('UPDATE styles SET md5Url = ? WHERE id = ?;', [o.md5Url, o.id]);
} }
if ("originalMd5" in o) { if ("originalMd5" in o) {
t.executeSql('UPDATE styles SET originalMd5 = ? WHERE id = ?;', [o.originalMd5, o.id]); t.executeSql('UPDATE styles SET originalMd5 = ? WHERE id = ?;', [o.originalMd5, o.id]);
} }
} else { } else {
// create a new record // create a new record
// set optional things to null if they're undefined // set optional things to null if they're undefined
["updateUrl", "md5Url", "url", "originalMd5"].filter(function(att) { ["updateUrl", "md5Url", "url", "originalMd5"].filter(function(att) {
return !(att in o); return !(att in o);
}).forEach(function(att) { }).forEach(function(att) {
o[att] = null; o[att] = null;
}); });
t.executeSql('INSERT INTO styles (name, enabled, url, updateUrl, md5Url, originalMd5) VALUES (?, ?, ?, ?, ?, ?);', [o.name, true, o.url, o.updateUrl, o.md5Url, o.originalMd5]); t.executeSql('INSERT INTO styles (name, enabled, url, updateUrl, md5Url, originalMd5) VALUES (?, ?, ?, ?, ?, ?);', [o.name, true, o.url, o.updateUrl, o.md5Url, o.originalMd5]);
} }
if ("sections" in o) { if ("sections" in o) {
if (o.id) { if (o.id) {
// clear existing records // clear existing records
t.executeSql('DELETE FROM section_meta WHERE section_id IN (SELECT id FROM sections WHERE style_id = ?);', [o.id]); t.executeSql('DELETE FROM section_meta WHERE section_id IN (SELECT id FROM sections WHERE style_id = ?);', [o.id]);
t.executeSql('DELETE FROM sections WHERE style_id = ?;', [o.id]); t.executeSql('DELETE FROM sections WHERE style_id = ?;', [o.id]);
} }
o.sections.forEach(function(section) { o.sections.forEach(function(section) {
if (o.id) { if (o.id) {
t.executeSql('INSERT INTO sections (style_id, code) VALUES (?, ?);', [o.id, section.code]); t.executeSql('INSERT INTO sections (style_id, code) VALUES (?, ?);', [o.id, section.code]);
} else { } else {
t.executeSql('INSERT INTO sections (style_id, code) SELECT id, ? FROM styles ORDER BY id DESC LIMIT 1;', [section.code]); t.executeSql('INSERT INTO sections (style_id, code) SELECT id, ? FROM styles ORDER BY id DESC LIMIT 1;', [section.code]);
} }
if (section.urls) { if (section.urls) {
section.urls.forEach(function(u) { section.urls.forEach(function(u) {
t.executeSql("INSERT INTO section_meta (section_id, name, value) SELECT id, 'url', ? FROM sections ORDER BY id DESC LIMIT 1;", [u]); t.executeSql("INSERT INTO section_meta (section_id, name, value) SELECT id, 'url', ? FROM sections ORDER BY id DESC LIMIT 1;", [u]);
}); });
} }
if (section.urlPrefixes) { if (section.urlPrefixes) {
section.urlPrefixes.forEach(function(u) { section.urlPrefixes.forEach(function(u) {
t.executeSql("INSERT INTO section_meta (section_id, name, value) SELECT id, 'url-prefix', ? FROM sections ORDER BY id DESC LIMIT 1;", [u]); t.executeSql("INSERT INTO section_meta (section_id, name, value) SELECT id, 'url-prefix', ? FROM sections ORDER BY id DESC LIMIT 1;", [u]);
}); });
} }
if (section.domains) { if (section.domains) {
section.domains.forEach(function(u) { section.domains.forEach(function(u) {
t.executeSql("INSERT INTO section_meta (section_id, name, value) SELECT id, 'domain', ? FROM sections ORDER BY id DESC LIMIT 1;", [u]); t.executeSql("INSERT INTO section_meta (section_id, name, value) SELECT id, 'domain', ? FROM sections ORDER BY id DESC LIMIT 1;", [u]);
}); });
} }
if (section.regexps) { if (section.regexps) {
section.regexps.forEach(function(u) { section.regexps.forEach(function(u) {
t.executeSql("INSERT INTO section_meta (section_id, name, value) SELECT id, 'regexp', ? FROM sections ORDER BY id DESC LIMIT 1;", [u]); t.executeSql("INSERT INTO section_meta (section_id, name, value) SELECT id, 'regexp', ? FROM sections ORDER BY id DESC LIMIT 1;", [u]);
}); });
} }
}); });
} }
}, reportError, function() {saveFromJSONComplete(o.id, callback)}); }, reportError, function() {saveFromJSONComplete(o.id, callback)});
}, reportError); }, reportError);
} }
function saveFromJSONComplete(id, callback) { function saveFromJSONComplete(id, callback) {
cachedStyles = null; cachedStyles = null;
if (id) { if (id) {
getStyles({method: "getStyles", id: id}, function(styles) { getStyles({method: "getStyles", id: id}, function(styles) {
saveFromJSONStyleReloaded("styleUpdated", styles[0], callback); saveFromJSONStyleReloaded("styleUpdated", styles[0], callback);
}); });
return; return;
} }
// we need to load the id for new ones // we need to load the id for new ones
getDatabase(function(db) { getDatabase(function(db) {
db.readTransaction(function (t) { db.readTransaction(function (t) {
t.executeSql('SELECT id FROM styles ORDER BY id DESC LIMIT 1', [], function(t, r) { t.executeSql('SELECT id FROM styles ORDER BY id DESC LIMIT 1', [], function(t, r) {
var id = r.rows.item(0).id; var id = r.rows.item(0).id;
getStyles({method: "getStyles", id: id}, function(styles) { getStyles({method: "getStyles", id: id}, function(styles) {
saveFromJSONStyleReloaded("styleAdded", styles[0], callback); saveFromJSONStyleReloaded("styleAdded", styles[0], callback);
}); });
}, reportError) }, reportError)
}, reportError) }, reportError)
}); });
} }
function saveFromJSONStyleReloaded(updateType, style, callback) { function saveFromJSONStyleReloaded(updateType, style, callback) {
notifyAllTabs({name:updateType, style: style}); notifyAllTabs({name:updateType, style: style});
if (callback) { if (callback) {
callback(style); callback(style);
} }
} }
function getDomains(url) { function getDomains(url) {
var d = /.*?:\/*([^\/]+)/.exec(url)[1]; var d = /.*?:\/*([^\/]+)/.exec(url)[1];
var domains = [d]; var domains = [d];
while (d.indexOf(".") != -1) { while (d.indexOf(".") != -1) {
d = d.substring(d.indexOf(".") + 1); d = d.substring(d.indexOf(".") + 1);
domains.push(d); domains.push(d);
} }
return domains; return domains;
} }
// Get the DB so that any first run actions will be performed immediately when the background page loads. // Get the DB so that any first run actions will be performed immediately when the background page loads.
getDatabase(function() {}, reportError); getDatabase(function() {}, reportError);

View File

@ -1,41 +1,41 @@
{ {
"name": "Stylish", "name": "Stylish",
"version": "1.2.2", "version": "1.2.2",
"description": "__MSG_description__", "description": "__MSG_description__",
"homepage_url": "http://userstyles.org", "homepage_url": "http://userstyles.org",
"manifest_version": 2, "manifest_version": 2,
"icons": { "icons": {
"16": "16.png", "16": "16.png",
"48": "48.png", "48": "48.png",
"128": "128.png" "128": "128.png"
}, },
"permissions": [ "permissions": [
"tabs", "tabs",
"http://userstyles.org/", "http://userstyles.org/",
"https://userstyles.org/" "https://userstyles.org/"
], ],
"background": { "background": {
"page": "background.html" "page": "background.html"
}, },
"content_scripts": [ "content_scripts": [
{ {
"matches": ["http://*/*", "https://*/*"], "matches": ["http://*/*", "https://*/*"],
"run_at": "document_start", "run_at": "document_start",
"all_frames": true, "all_frames": true,
"js": ["apply.js"] "js": ["apply.js"]
}, },
{ {
"matches": ["http://userstyles.org/*", "https://userstyles.org/*"], "matches": ["http://userstyles.org/*", "https://userstyles.org/*"],
"run_at": "document_end", "run_at": "document_end",
"all_frames": false, "all_frames": false,
"js": ["install.js"] "js": ["install.js"]
} }
], ],
"options_page": "manage.html", "options_page": "manage.html",
"browser_action": { "browser_action": {
"default_icon": "19.png", "default_icon": "19.png",
"default_title": "Stylish", "default_title": "Stylish",
"default_popup": "popup.html" "default_popup": "popup.html"
}, },
"default_locale": "en" "default_locale": "en"
} }