2012-04-16 01:56:12 +00:00
|
|
|
var styleId = null;
|
|
|
|
var dirty = false;
|
|
|
|
|
|
|
|
var appliesToTemplate = document.createElement("li");
|
2012-08-20 01:14:33 +00:00
|
|
|
appliesToTemplate.innerHTML = '<select name="applies-type" class="applies-type"><option value="url">' + t("appliesUrlOption") + '</option><option value="url-prefix">' + t("appliesUrlPrefixOption") + '</option><option value="domain">' + t("appliesDomainOption") + '</option><option value="regexp">' + t("appliesRegexpOption") + '</option></select><input name="applies-value" class="applies-value"><button class="remove-applies-to">' + t("appliesRemove") + '</button><button class="add-applies-to">' + t("appliesAdd") + '</button>';
|
2012-04-16 01:56:12 +00:00
|
|
|
|
|
|
|
var appliesToEverythingTemplate = document.createElement("li");
|
|
|
|
appliesToEverythingTemplate.className = "applies-to-everything";
|
2012-08-20 01:14:33 +00:00
|
|
|
appliesToEverythingTemplate.innerHTML = t("appliesToEverything") + ' <button class="add-applies-to">' + t("appliesSpecify") + '</button>'
|
2012-04-16 01:56:12 +00:00
|
|
|
|
|
|
|
var sectionTemplate = document.createElement("div");
|
2012-08-20 01:14:33 +00:00
|
|
|
sectionTemplate.innerHTML = '<label>' + t('sectionCode') + '</label><textarea class="code"></textarea><br><div class="applies-to"><label>' + t("appliesLabel") + ' <img class="applies-to-help" src="help.png" alt="' + t('helpAlt') + '"></label><ul class="applies-to-list"></ul></div><button class="remove-section">' + t('sectionRemove') + '</button><button class="add-section">' + t('sectionAdd') + '</button>';
|
2012-04-16 01:56:12 +00:00
|
|
|
|
|
|
|
function makeDirty() {
|
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.onbeforeunload = function() {
|
|
|
|
return dirty ? t('styleChangesNotSaved') : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addAppliesTo(list, name, value) {
|
|
|
|
var showingEverything = list.querySelector(".applies-to-everything") != null;
|
|
|
|
// blow away "Everything" if it's there
|
|
|
|
if (showingEverything) {
|
|
|
|
list.removeChild(list.firstChild);
|
|
|
|
}
|
|
|
|
var e;
|
|
|
|
if (name && value) {
|
|
|
|
e = appliesToTemplate.cloneNode(true);
|
|
|
|
e.querySelector("[name=applies-type]").value = name;
|
|
|
|
e.querySelector("[name=applies-value]").value = value;
|
2012-08-20 01:14:33 +00:00
|
|
|
e.querySelector(".remove-applies-to").addEventListener("click", removeAppliesTo, false);
|
|
|
|
e.querySelector(".applies-value").addEventListener("change", makeDirty, false);
|
|
|
|
e.querySelector(".applies-type").addEventListener("change", makeDirty, false);
|
2012-04-16 01:56:12 +00:00
|
|
|
} else if (showingEverything || list.hasChildNodes()) {
|
|
|
|
e = appliesToTemplate.cloneNode(true);
|
|
|
|
if (list.hasChildNodes()) {
|
|
|
|
e.querySelector("[name=applies-type]").value = list.querySelector("li:last-child [name='applies-type']").value;
|
|
|
|
}
|
2012-08-20 01:14:33 +00:00
|
|
|
e.querySelector(".remove-applies-to").addEventListener("click", removeAppliesTo, false);
|
|
|
|
e.querySelector(".applies-value").addEventListener("change", makeDirty, false);
|
|
|
|
e.querySelector(".applies-type").addEventListener("change", makeDirty, false);
|
2012-04-16 01:56:12 +00:00
|
|
|
} else {
|
|
|
|
e = appliesToEverythingTemplate.cloneNode(true);
|
|
|
|
}
|
2012-08-20 01:14:33 +00:00
|
|
|
e.querySelector(".add-applies-to").addEventListener("click", function() {addAppliesTo(this.parentNode.parentNode)}, false);
|
2012-04-16 01:56:12 +00:00
|
|
|
list.appendChild(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addSection(section) {
|
|
|
|
var div = sectionTemplate.cloneNode(true);
|
2012-08-20 01:14:33 +00:00
|
|
|
div.querySelector(".applies-to-help").addEventListener("click", showAppliesToHelp, false);
|
|
|
|
div.querySelector(".remove-section").addEventListener("click", removeSection, false);
|
|
|
|
div.querySelector(".add-section").addEventListener("click", function() {addSection()}, false);
|
|
|
|
|
2012-04-16 01:56:12 +00:00
|
|
|
var appliesTo = div.querySelector(".applies-to-list");
|
|
|
|
|
|
|
|
if (section) {
|
2012-08-20 01:14:33 +00:00
|
|
|
var codeElement = div.querySelector(".code");
|
|
|
|
codeElement.value = section.code;
|
|
|
|
codeElement.addEventListener("change", makeDirty, false);
|
2012-04-16 01:56:12 +00:00
|
|
|
if (section.urls) {
|
|
|
|
section.urls.forEach(function(url) {
|
|
|
|
addAppliesTo(appliesTo, "url", url);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (section.urlPrefixes) {
|
|
|
|
section.urlPrefixes.forEach(function(url) {
|
|
|
|
addAppliesTo(appliesTo, "url-prefix", url);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (section.domains) {
|
|
|
|
section.domains.forEach(function(d) {
|
|
|
|
addAppliesTo(appliesTo, "domain", d);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (section.regexps) {
|
|
|
|
section.regexps.forEach(function(d) {
|
|
|
|
addAppliesTo(appliesTo, "regexp", d);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!section.urls && !section.urlPrefixes && !section.domains && !section.regexps) {
|
|
|
|
addAppliesTo(appliesTo);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addAppliesTo(appliesTo);
|
|
|
|
}
|
|
|
|
|
|
|
|
var sections = document.getElementById("sections");
|
|
|
|
sections.appendChild(div);
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeAppliesTo(event) {
|
|
|
|
var appliesToList = event.target.parentNode.parentNode;
|
|
|
|
appliesToList.removeChild(event.target.parentNode);
|
|
|
|
if (!appliesToList.hasChildNodes()) {
|
2012-08-20 01:14:33 +00:00
|
|
|
var e = appliesToEverythingTemplate.cloneNode(true);
|
|
|
|
e.querySelector(".add-applies-to").addEventListener("click", function() {addAppliesTo(this.parentNode.parentNode)}, false);
|
|
|
|
appliesToList.appendChild(e);
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
|
|
|
makeDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeSection(event) {
|
|
|
|
event.target.parentNode.parentNode.removeChild(event.target.parentNode);
|
|
|
|
makeDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener("load", init, false);
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
tE("sections-help", "helpAlt", "alt");
|
|
|
|
var idMatch = /[&\?]id=([0-9]+)/.exec(location.href)
|
|
|
|
if (idMatch == null || idMatch.length != 2) { // match should be 2 - one for the whole thing, one for the parentheses
|
|
|
|
// This is an add
|
|
|
|
addSection();
|
|
|
|
document.title = t("addStyleTitle");
|
|
|
|
tE("heading", "addStyleTitle");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// This is an edit
|
|
|
|
var id = idMatch[1];
|
2012-08-20 01:14:33 +00:00
|
|
|
chrome.extension.sendMessage({method: "getStyles", id: id}, function(styles) {
|
2012-04-16 01:56:12 +00:00
|
|
|
var style = styles[0];
|
|
|
|
styleId = style.id;
|
|
|
|
initWithStyle(style);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initWithStyle(style) {
|
|
|
|
document.getElementById("name").value = style.name;
|
|
|
|
document.getElementById("enabled").checked = style.enabled == "true";
|
|
|
|
document.getElementById("heading").innerHTML = t("editStyleHeading");
|
2012-08-20 01:14:33 +00:00
|
|
|
initTitle(style.name);
|
2012-04-16 01:56:12 +00:00
|
|
|
// if this was done in response to an update, we need to clear existing sections
|
|
|
|
Array.prototype.forEach.call(document.querySelectorAll("#sections > div"), function(div) {
|
|
|
|
div.parentNode.removeChild(div);
|
|
|
|
});
|
|
|
|
style.sections.forEach(addSection);
|
|
|
|
}
|
|
|
|
|
2012-08-20 01:14:33 +00:00
|
|
|
function initTitle(name) {
|
|
|
|
document.title = t('editStyleTitle', [name]);
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function validate() {
|
|
|
|
var name = document.getElementById("name").value;
|
|
|
|
if (name == "") {
|
|
|
|
return t("styleMissingName");
|
|
|
|
}
|
2012-11-03 03:25:04 +00:00
|
|
|
// validate the regexps
|
|
|
|
if (Array.prototype.some.call(document.querySelectorAll(".applies-to-list"), function(list) {
|
|
|
|
return Array.prototype.some.call(list.childNodes, function(li) {
|
|
|
|
if (li.className == appliesToEverythingTemplate.className) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var valueElement = li.querySelector("[name=applies-value]");
|
|
|
|
var a = li.querySelector("[name=applies-type]").value;
|
|
|
|
var b = valueElement.value;
|
|
|
|
if (a && b) {
|
|
|
|
if (a == "regexp") {
|
|
|
|
try {
|
|
|
|
new RegExp(b);
|
|
|
|
} catch (ex) {
|
|
|
|
valueElement.focus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
})) {
|
|
|
|
return t("styleBadRegexp");
|
|
|
|
}
|
2012-04-16 01:56:12 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function save() {
|
|
|
|
var error = validate();
|
|
|
|
if (error) {
|
|
|
|
alert(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var name = document.getElementById("name").value;
|
|
|
|
var enabled = document.getElementById("enabled").checked;
|
2012-08-20 01:14:33 +00:00
|
|
|
var request = {
|
|
|
|
method: "saveStyle",
|
|
|
|
id: styleId,
|
|
|
|
name: name,
|
|
|
|
enabled: enabled,
|
|
|
|
sections: getSections(),
|
|
|
|
};
|
|
|
|
chrome.extension.sendMessage(request, saveComplete);
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getSections() {
|
|
|
|
var sections = [];
|
|
|
|
Array.prototype.forEach.call(document.querySelectorAll("#sections > div"), function(div) {
|
|
|
|
var code = div.querySelector(".code").value;
|
|
|
|
if (/^\s*$/.test(code)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-20 01:14:33 +00:00
|
|
|
var meta = getMeta(div);
|
|
|
|
meta.code = code;
|
|
|
|
sections.push(meta);
|
2012-04-16 01:56:12 +00:00
|
|
|
});
|
|
|
|
return sections;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMeta(e) {
|
2012-08-20 01:14:33 +00:00
|
|
|
var meta = {urls: [], urlPrefixes: [], domains: [], regexps: []};
|
2012-04-16 01:56:12 +00:00
|
|
|
Array.prototype.forEach.call(e.querySelector(".applies-to-list").childNodes, function(li) {
|
|
|
|
if (li.className == appliesToEverythingTemplate.className) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var a = li.querySelector("[name=applies-type]").value;
|
|
|
|
var b = li.querySelector("[name=applies-value]").value;
|
|
|
|
if (a && b) {
|
2012-08-20 01:14:33 +00:00
|
|
|
switch (a) {
|
|
|
|
case "url":
|
|
|
|
meta.urls.push(b);
|
|
|
|
break;
|
|
|
|
case "url-prefix":
|
|
|
|
meta.urlPrefixes.push(b);
|
|
|
|
break;
|
|
|
|
case "domain":
|
|
|
|
meta.domains.push(b);
|
|
|
|
break;
|
|
|
|
case "regexp":
|
|
|
|
meta.regexps.push(b);
|
|
|
|
break;
|
|
|
|
}
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return meta;
|
|
|
|
}
|
|
|
|
|
2012-09-01 19:43:52 +00:00
|
|
|
function saveComplete(style) {
|
|
|
|
dirty = false;
|
2012-08-20 01:14:33 +00:00
|
|
|
// Go from new style URL to edit style URL
|
|
|
|
if (location.href.indexOf("id=") == -1) {
|
|
|
|
// give the code above a moment before we kill the page
|
2012-09-01 19:43:52 +00:00
|
|
|
setTimeout(function() {location.href = "edit.html?id=" + style.id;}, 200);
|
2012-08-20 01:14:33 +00:00
|
|
|
} else {
|
|
|
|
initTitle(document.getElementById("name").value);
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showMozillaFormat() {
|
2012-08-07 03:00:23 +00:00
|
|
|
var w = window.open("data:text/plain;charset=UTF-8," + encodeURIComponent(toMozillaFormat()));
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function toMozillaFormat() {
|
|
|
|
return getSections().map(function(section) {
|
|
|
|
if (section.meta.length == 0) {
|
|
|
|
return section.code;
|
|
|
|
}
|
|
|
|
var mf = "@-moz-document ";
|
|
|
|
mf += section.meta.map(function(meta) {
|
2012-05-06 03:48:03 +00:00
|
|
|
// escape the meta according to css rules
|
|
|
|
return meta[0] + "(\"" + meta[1].replace(/\\/g, "\\\\") + "\")";
|
2012-04-16 01:56:12 +00:00
|
|
|
}).join(", ");
|
|
|
|
return mf + " {\n" + section.code + "\n}";
|
|
|
|
}).join("\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
function showSectionHelp() {
|
|
|
|
showHelp(t("sectionHelp"));
|
|
|
|
}
|
|
|
|
|
|
|
|
function showAppliesToHelp() {
|
|
|
|
showHelp(t("appliesHelp"));
|
|
|
|
}
|
|
|
|
|
|
|
|
function showToMozillaHelp() {
|
|
|
|
showHelp(t("styleToMozillaFormatHelp"));
|
|
|
|
}
|
|
|
|
|
|
|
|
function showHelp(text) {
|
|
|
|
alert(text);
|
|
|
|
}
|
|
|
|
|
2012-08-20 01:14:33 +00:00
|
|
|
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
|
2012-04-16 01:56:12 +00:00
|
|
|
var installed = document.getElementById("installed");
|
|
|
|
switch(request.name) {
|
|
|
|
case "styleUpdated":
|
2012-09-01 19:43:52 +00:00
|
|
|
if (styleId == request.id) {
|
|
|
|
initWithStyle(request.style);
|
|
|
|
dirty = false;
|
|
|
|
}
|
2012-04-16 01:56:12 +00:00
|
|
|
break;
|
|
|
|
case "styleDeleted":
|
|
|
|
if (styleId == request.id) {
|
|
|
|
window.close();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-08-20 01:14:33 +00:00
|
|
|
|
|
|
|
tE("name-label", "styleNameLabel");
|
|
|
|
tE("enabled-label", "styleEnabledLabel");
|
|
|
|
tE("to-mozilla", "styleToMozillaFormat");
|
|
|
|
tE("save-button", "styleSaveLabel");
|
|
|
|
tE("cancel-button", "styleCancelEditLabel");
|
|
|
|
tE("sections-heading", "styleSectionsTitle");
|
|
|
|
document.getElementById("name").addEventListener("change", makeDirty, false);
|
|
|
|
document.getElementById("enabled").addEventListener("change", makeDirty, false);
|
|
|
|
document.getElementById("to-mozilla").addEventListener("click", showMozillaFormat, false);
|
|
|
|
document.getElementById("to-mozilla-help").addEventListener("click", showToMozillaHelp, false);
|
|
|
|
document.getElementById("save-button").addEventListener("click", save, false);
|
|
|
|
document.getElementById("sections-help").addEventListener("click", showSectionHelp, false);
|
|
|
|
|