2017-11-07 00:36:44 +01:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
(function(){
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var scope;
|
|
|
|
if ((typeof exports) !== "undefined"){
|
|
|
|
scope = exports;
|
|
|
|
}
|
|
|
|
else {
|
2019-03-12 22:24:23 +01:00
|
|
|
scope = require.register("./optionsGui", {});
|
2017-11-07 00:36:44 +01:00
|
|
|
}
|
2019-04-09 08:29:52 +02:00
|
|
|
|
|
|
|
const extension = require("../lib/extension");
|
2019-04-08 00:02:29 +02:00
|
|
|
const logging = require("../lib/logging");
|
2017-11-07 00:36:44 +01:00
|
|
|
|
|
|
|
function createDescription(setting){
|
|
|
|
var c = document.createElement("div");
|
|
|
|
c.className = "content";
|
|
|
|
|
|
|
|
var title = document.createElement("span");
|
|
|
|
title.className = "title";
|
2019-04-09 08:29:52 +02:00
|
|
|
title.textContent = extension.getTranslation(setting.name + "_title");
|
2017-11-07 00:36:44 +01:00
|
|
|
c.appendChild(title);
|
|
|
|
|
2019-04-09 08:29:52 +02:00
|
|
|
var descriptionText = extension.getTranslation(setting.name + "_description");
|
2018-09-12 22:36:14 +02:00
|
|
|
if (setting.urlSpecific){
|
2019-04-09 08:29:52 +02:00
|
|
|
const urlSpecificDescription = extension.getTranslation(setting.name + "_urlSpecific");
|
2018-09-12 22:36:14 +02:00
|
|
|
if (urlSpecificDescription){
|
|
|
|
descriptionText += (descriptionText? "\n\n": "") + urlSpecificDescription;
|
|
|
|
}
|
|
|
|
}
|
2017-11-11 23:34:59 +01:00
|
|
|
if (descriptionText){
|
|
|
|
var info = document.createElement("div");
|
|
|
|
info.className = "info";
|
|
|
|
c.appendChild(info);
|
2017-11-07 00:36:44 +01:00
|
|
|
|
2017-11-11 23:34:59 +01:00
|
|
|
var description = document.createElement("div");
|
|
|
|
description.className = "description";
|
|
|
|
description.textContent = descriptionText;
|
|
|
|
info.appendChild(description);
|
|
|
|
}
|
2017-11-07 00:36:44 +01:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createSelect(setting){
|
|
|
|
var select = document.createElement("select");
|
|
|
|
select.dataset.type = typeof setting.defaultValue;
|
|
|
|
setting.options.forEach(function(value){
|
|
|
|
var option = document.createElement("option");
|
|
|
|
if (typeof value === typeof setting.defaultValue){
|
|
|
|
option.value = value;
|
|
|
|
if (setting.defaultValue === value){
|
|
|
|
option.selected = true;
|
|
|
|
}
|
2019-04-09 08:29:52 +02:00
|
|
|
option.text = extension.getTranslation(setting.name + "_options." + value) || value;
|
2017-11-07 00:36:44 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
option.disabled = true;
|
|
|
|
option.text = "\u2500".repeat(20);
|
|
|
|
}
|
|
|
|
select.appendChild(option);
|
|
|
|
});
|
|
|
|
return select;
|
|
|
|
}
|
|
|
|
|
|
|
|
var inputTypes = {
|
|
|
|
number: {
|
2019-04-25 23:39:52 +02:00
|
|
|
input: function(value){
|
|
|
|
const input = document.createElement("input");
|
2017-11-07 00:36:44 +01:00
|
|
|
input.type = "number";
|
|
|
|
input.value = value;
|
|
|
|
return input;
|
|
|
|
},
|
2017-12-03 23:47:49 +01:00
|
|
|
updateCallback: function(input, value){
|
|
|
|
input.value = value;
|
2017-11-07 00:36:44 +01:00
|
|
|
return input.value;
|
|
|
|
},
|
2017-12-03 23:47:49 +01:00
|
|
|
getValue: function(input){
|
2017-11-07 00:36:44 +01:00
|
|
|
return parseFloat(input.value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
string: {
|
2019-04-25 23:39:52 +02:00
|
|
|
input: function(value, setting){
|
|
|
|
let input;
|
|
|
|
if (setting && setting.display && setting.display.multiline){
|
|
|
|
input = document.createElement("textarea");
|
|
|
|
input.rows = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
input = document.createElement("input");
|
|
|
|
input.type = "text";
|
|
|
|
}
|
2017-11-07 00:36:44 +01:00
|
|
|
input.value = value;
|
|
|
|
return input;
|
|
|
|
},
|
2017-12-03 23:47:49 +01:00
|
|
|
updateCallback: function(input, value){
|
|
|
|
input.value = value;
|
2017-11-07 00:36:44 +01:00
|
|
|
return input.value;
|
|
|
|
},
|
2017-12-03 23:47:49 +01:00
|
|
|
getValue: function(input){
|
2017-11-07 00:36:44 +01:00
|
|
|
return input.value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
boolean: {
|
2019-04-25 23:39:52 +02:00
|
|
|
input: function(value){
|
|
|
|
const input = document.createElement("input");
|
2017-11-07 00:36:44 +01:00
|
|
|
input.type = "checkbox";
|
|
|
|
input.checked = value;
|
2019-05-02 00:30:30 +02:00
|
|
|
input.style.display = "inline-block";
|
2017-11-07 00:36:44 +01:00
|
|
|
return input;
|
|
|
|
},
|
2017-12-03 23:47:49 +01:00
|
|
|
updateCallback: function(input, value){
|
|
|
|
input.checked = value;
|
2017-11-07 00:36:44 +01:00
|
|
|
return input.checked;
|
|
|
|
},
|
2017-12-03 23:47:49 +01:00
|
|
|
getValue: function(input){
|
2017-11-07 00:36:44 +01:00
|
|
|
return input.checked;
|
|
|
|
}
|
2017-12-03 23:47:49 +01:00
|
|
|
},
|
|
|
|
object: false
|
2017-11-07 00:36:44 +01:00
|
|
|
};
|
|
|
|
|
2017-12-03 23:47:49 +01:00
|
|
|
function createInput(setting, url = ""){
|
2017-11-07 00:36:44 +01:00
|
|
|
var type = inputTypes[typeof setting.defaultValue];
|
|
|
|
var input;
|
|
|
|
if (setting.options){
|
|
|
|
input = createSelect(setting);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (type){
|
2019-04-25 23:39:52 +02:00
|
|
|
input = type.input(setting.defaultValue, setting);
|
2017-11-07 00:36:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type){
|
2017-12-03 23:47:49 +01:00
|
|
|
setting.on(function(){type.updateCallback(input, setting.get(url));}, url);
|
2017-11-07 00:36:44 +01:00
|
|
|
input.addEventListener("change", function(){
|
2017-12-03 23:47:49 +01:00
|
|
|
var value = type.getValue(input);
|
2018-10-25 13:42:18 +02:00
|
|
|
if (setting.set(value, url)){
|
|
|
|
logging.message("changed setting", setting.name, ":", value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
type.updateCallback(input, setting.get(url));
|
|
|
|
logging.message("setting", setting.name, "was not changed");
|
|
|
|
}
|
2017-11-07 00:36:44 +01:00
|
|
|
});
|
|
|
|
}
|
2018-05-26 15:33:39 +02:00
|
|
|
else if (setting.keys){
|
|
|
|
input = document.createElement("table");
|
2018-09-12 09:21:52 +02:00
|
|
|
let inSection = false;
|
2018-05-26 15:33:39 +02:00
|
|
|
setting.keys.forEach(function(key){
|
2018-09-12 09:21:52 +02:00
|
|
|
if (setting.display.displayedSection){
|
|
|
|
if (typeof key === "object"){
|
|
|
|
if (key.level === 1){
|
|
|
|
inSection = key.name === setting.display.displayedSection;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!inSection){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-05-26 15:33:39 +02:00
|
|
|
let row = document.createElement("tr");
|
2018-06-21 00:07:29 +02:00
|
|
|
if (typeof key === "object"){
|
|
|
|
let cell = document.createElement("td");
|
|
|
|
cell.colSpan = 2;
|
|
|
|
let h = document.createElement("h" + (2 + (key.level || 1)));
|
2019-04-09 08:29:52 +02:00
|
|
|
h.textContent = key.message? extension.getTranslation(key.message): key.name;
|
2018-06-21 00:07:29 +02:00
|
|
|
cell.appendChild(h);
|
|
|
|
row.appendChild(cell);
|
|
|
|
input.appendChild(row);
|
|
|
|
return;
|
|
|
|
}
|
2018-05-26 15:33:39 +02:00
|
|
|
|
|
|
|
let nameCell = document.createElement("td");
|
|
|
|
nameCell.textContent = key;
|
|
|
|
row.appendChild(nameCell);
|
|
|
|
|
|
|
|
let keyType = inputTypes[typeof setting.defaultKeyValue];
|
2019-04-25 23:39:52 +02:00
|
|
|
let keyInput = keyType.input(setting.defaultKeyValue);
|
2018-05-26 15:33:39 +02:00
|
|
|
|
|
|
|
let inputCell = document.createElement("td");
|
|
|
|
inputCell.appendChild(keyInput);
|
|
|
|
row.appendChild(inputCell);
|
|
|
|
|
|
|
|
setting.on(function(){
|
|
|
|
var container = setting.get(url);
|
|
|
|
keyType.updateCallback(
|
|
|
|
keyInput,
|
|
|
|
container && container.hasOwnProperty(key)?
|
|
|
|
container[key]:
|
|
|
|
setting.defaultKeyValue,
|
|
|
|
url
|
|
|
|
);
|
|
|
|
});
|
|
|
|
keyInput.addEventListener("change", function(){
|
|
|
|
var value = keyType.getValue(keyInput);
|
|
|
|
var container = setting.get(url);
|
|
|
|
if (!container){
|
|
|
|
container = setting.defaultValue;
|
|
|
|
}
|
|
|
|
container[key] = value;
|
2018-10-25 13:42:18 +02:00
|
|
|
if (setting.set(container, url)){
|
|
|
|
logging.message("changed setting", setting.name, "(", key, "):", value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
container = setting.get(url);
|
|
|
|
keyType.updateCallback(
|
|
|
|
keyInput,
|
|
|
|
container && container.hasOwnProperty(key)?
|
|
|
|
container[key]:
|
|
|
|
setting.defaultKeyValue,
|
|
|
|
url
|
|
|
|
);
|
|
|
|
logging.message("setting", setting.name, "(", key, ") was not changed");
|
|
|
|
}
|
2018-05-26 15:33:39 +02:00
|
|
|
});
|
|
|
|
input.appendChild(row);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-03 23:47:49 +01:00
|
|
|
if (setting.urlSpecific && url === ""){
|
|
|
|
let container = document.createElement("div");
|
2018-09-12 23:43:48 +02:00
|
|
|
container.className = "urlValues " + (setting.getExpand()? "expanded": "collapsed");
|
2017-12-03 23:47:49 +01:00
|
|
|
container.appendChild(input);
|
2018-10-16 00:11:07 +02:00
|
|
|
var collapser = document.createElement("button");
|
2017-12-03 23:47:49 +01:00
|
|
|
collapser.classList.add("collapser");
|
|
|
|
container.appendChild(collapser);
|
|
|
|
collapser.addEventListener("click", function(){
|
2018-09-12 23:43:48 +02:00
|
|
|
setting.setExpand(!setting.getExpand());
|
|
|
|
});
|
|
|
|
setting.onExpandChange(function(value){
|
|
|
|
container.classList[value? "remove": "add"]("collapsed");
|
|
|
|
container.classList[value? "add": "remove"]("expanded");
|
2017-12-03 23:47:49 +01:00
|
|
|
});
|
|
|
|
let urlTable = document.createElement("table");
|
|
|
|
let caption = document.createElement("caption");
|
2019-04-09 08:29:52 +02:00
|
|
|
caption.textContent = extension.getTranslation(setting.urlContainer.name + "_title");
|
2017-12-03 23:47:49 +01:00
|
|
|
urlTable.appendChild(caption);
|
|
|
|
let body = document.createElement("tbody");
|
|
|
|
urlTable.appendChild(body);
|
|
|
|
let foot = document.createElement("tfoot");
|
|
|
|
let footRow = document.createElement("tr");
|
|
|
|
let footCell = document.createElement("td");
|
|
|
|
footCell.colSpan = 3;
|
2017-12-08 20:20:42 +01:00
|
|
|
let newInput = document.createElement("input");
|
2019-08-21 14:33:29 +02:00
|
|
|
newInput.className = "inputURL";
|
2019-04-09 08:29:52 +02:00
|
|
|
newInput.title = extension.getTranslation("inputURL");
|
2018-10-15 08:53:07 +02:00
|
|
|
const addURLSetting = function(){
|
2017-12-08 20:20:42 +01:00
|
|
|
var url = newInput.value.trim();
|
2017-12-03 23:47:49 +01:00
|
|
|
if (url){
|
|
|
|
setting.set(setting.get(url), url);
|
2017-12-08 20:20:42 +01:00
|
|
|
newInput.value = "";
|
2018-10-16 00:11:07 +02:00
|
|
|
newInput.focus();
|
2017-12-03 23:47:49 +01:00
|
|
|
}
|
2018-10-15 08:53:07 +02:00
|
|
|
};
|
|
|
|
newInput.addEventListener("keypress", function(event){
|
|
|
|
if ([10, 13].indexOf(event.keyCode) !== -1){
|
|
|
|
addURLSetting();
|
|
|
|
}
|
2017-12-03 23:47:49 +01:00
|
|
|
});
|
2018-10-15 08:53:07 +02:00
|
|
|
footCell.appendChild(newInput);
|
2018-10-16 00:11:07 +02:00
|
|
|
let footPlus = document.createElement("button");
|
2018-10-15 08:53:07 +02:00
|
|
|
footPlus.classList.add("add");
|
|
|
|
footPlus.textContent = "+";
|
|
|
|
footPlus.addEventListener("click", addURLSetting);
|
2017-12-03 23:47:49 +01:00
|
|
|
footCell.appendChild(footPlus);
|
|
|
|
footRow.appendChild(footCell);
|
|
|
|
foot.appendChild(footRow);
|
|
|
|
urlTable.appendChild(foot);
|
|
|
|
container.appendChild(urlTable);
|
|
|
|
|
|
|
|
setting.urlContainer.on(function({newValue}){
|
|
|
|
body.innerHTML = "";
|
|
|
|
newValue.forEach(function(entry){
|
|
|
|
let row = document.createElement("tr");
|
|
|
|
let urlCell = document.createElement("td");
|
|
|
|
urlCell.classList.add("url");
|
|
|
|
urlCell.addEventListener("click", function(){
|
2017-12-08 20:20:42 +01:00
|
|
|
var input = document.createElement("input");
|
|
|
|
input.classList.add("urlInput");
|
|
|
|
input.style.width = urlCell.clientWidth + "px";
|
|
|
|
input.style.height = urlCell.clientHeight + "px";
|
|
|
|
urlCell.innerHTML = "";
|
|
|
|
urlCell.appendChild(input);
|
2019-04-09 08:29:52 +02:00
|
|
|
input.title = extension.getTranslation("inputURL");
|
2017-12-08 20:20:42 +01:00
|
|
|
input.value = entry.url;
|
|
|
|
input.focus();
|
|
|
|
input.addEventListener("blur", function(){
|
|
|
|
var url = input.value.trim();
|
|
|
|
if (url){
|
|
|
|
entry.url = url;
|
|
|
|
setting.urlContainer.refresh();
|
|
|
|
}
|
|
|
|
urlCell.removeChild(input);
|
|
|
|
urlCell.textContent = entry.url;
|
|
|
|
});
|
2017-12-03 23:47:49 +01:00
|
|
|
});
|
|
|
|
urlCell.textContent = entry.url;
|
|
|
|
row.appendChild(urlCell);
|
|
|
|
let input = createInput(setting, entry.url);
|
|
|
|
type.updateCallback(input, setting.get(entry.url));
|
|
|
|
if (!entry.hasOwnProperty(setting.name)){
|
|
|
|
input.classList.add("notSpecifiedForUrl");
|
|
|
|
}
|
|
|
|
let inputCell = document.createElement("td");
|
|
|
|
inputCell.appendChild(input);
|
|
|
|
row.appendChild(inputCell);
|
|
|
|
let clearCell = document.createElement("td");
|
2018-10-16 00:11:07 +02:00
|
|
|
let clearButton = document.createElement("button");
|
|
|
|
clearButton.className = "reset";
|
|
|
|
clearButton.textContent = "\xD7";
|
|
|
|
clearButton.addEventListener("click", function(){
|
2017-12-03 23:47:49 +01:00
|
|
|
setting.reset(entry.url);
|
|
|
|
});
|
2018-10-16 00:11:07 +02:00
|
|
|
clearCell.appendChild(clearButton);
|
2017-12-03 23:47:49 +01:00
|
|
|
row.appendChild(clearCell);
|
|
|
|
body.appendChild(row);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return container;
|
|
|
|
}
|
|
|
|
return input || document.createElement("span");
|
2017-11-07 00:36:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function createButton(setting){
|
|
|
|
var button = document.createElement("button");
|
2019-04-09 08:29:52 +02:00
|
|
|
button.textContent = extension.getTranslation(setting.name + "_label");
|
2017-11-07 00:36:44 +01:00
|
|
|
button.addEventListener("click", setting.action);
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createInteraction(setting){
|
|
|
|
var c = document.createElement("div");
|
|
|
|
c.className = "content";
|
|
|
|
|
|
|
|
var interaction;
|
|
|
|
if (setting.action){
|
|
|
|
interaction = createButton(setting);
|
|
|
|
}
|
2018-09-02 13:34:20 +02:00
|
|
|
else if (setting.actions){
|
|
|
|
interaction = document.createElement("span");
|
|
|
|
setting.actions.forEach(function(action){
|
|
|
|
var button = createButton(action);
|
|
|
|
interaction.appendChild(button);
|
|
|
|
});
|
|
|
|
}
|
2017-11-08 17:46:41 +01:00
|
|
|
else if (setting.inputs){
|
|
|
|
interaction = document.createElement("span");
|
|
|
|
setting.inputs.forEach(function(inputSetting){
|
|
|
|
var input = createInput(inputSetting);
|
|
|
|
input.classList.add("multiple" + setting.inputs.length);
|
|
|
|
interaction.appendChild(input);
|
|
|
|
});
|
|
|
|
}
|
2017-11-07 00:36:44 +01:00
|
|
|
else {
|
|
|
|
interaction = createInput(setting);
|
|
|
|
}
|
|
|
|
|
2017-12-03 23:47:49 +01:00
|
|
|
interaction.classList.add("setting");
|
2017-11-07 00:36:44 +01:00
|
|
|
interaction.dataset.storageName = setting.name;
|
|
|
|
interaction.dataset.storageType = typeof setting.defaultValue;
|
|
|
|
|
|
|
|
c.appendChild(interaction);
|
|
|
|
return c;
|
|
|
|
}
|
2018-07-02 00:29:41 +02:00
|
|
|
|
|
|
|
function createHide(setting){
|
|
|
|
var label = document.createElement("label");
|
|
|
|
label.className = "content hideContent";
|
2019-04-09 08:29:52 +02:00
|
|
|
label.title = extension.getTranslation("hideSetting");
|
2018-07-02 00:29:41 +02:00
|
|
|
var input = document.createElement("input");
|
|
|
|
input.type = "checkbox";
|
|
|
|
input.className = "hide";
|
|
|
|
input.checked = setting.getHide();
|
|
|
|
input.addEventListener("change", function(){
|
|
|
|
setting.setHide(this.checked);
|
|
|
|
});
|
|
|
|
setting.onHideChange(function(value){
|
|
|
|
input.checked = value;
|
|
|
|
});
|
|
|
|
|
|
|
|
label.appendChild(input);
|
|
|
|
var display = document.createElement("span");
|
|
|
|
display.className = "display";
|
|
|
|
label.appendChild(display);
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
2017-11-07 00:36:44 +01:00
|
|
|
function createSettingRow(setting){
|
|
|
|
var tr = document.createElement("tr");
|
|
|
|
tr.className = "settingRow";
|
2018-07-02 00:29:41 +02:00
|
|
|
|
|
|
|
var hide = document.createElement("td");
|
|
|
|
hide.className = "hideColumn";
|
|
|
|
hide.appendChild(createHide(setting));
|
|
|
|
tr.appendChild(hide);
|
2017-11-07 00:36:44 +01:00
|
|
|
|
|
|
|
var left = document.createElement("td");
|
|
|
|
left.appendChild(createDescription(setting));
|
|
|
|
tr.appendChild(left);
|
|
|
|
|
|
|
|
var right = document.createElement("td");
|
|
|
|
right.appendChild(createInteraction(setting));
|
|
|
|
tr.appendChild(right);
|
|
|
|
|
|
|
|
return tr;
|
|
|
|
}
|
|
|
|
|
|
|
|
scope.createSettingRow = createSettingRow;
|
2018-07-02 00:29:41 +02:00
|
|
|
|
2018-09-18 13:14:39 +02:00
|
|
|
function createThead(displayHidden, restContent){
|
2018-07-02 00:29:41 +02:00
|
|
|
const tHead = document.createElement("thead");
|
2019-05-20 08:55:19 +02:00
|
|
|
const searchRow = document.createElement("tr");
|
2018-07-02 00:29:41 +02:00
|
|
|
const hideHeadCell = document.createElement("td");
|
|
|
|
hideHeadCell.className = "hideColumn";
|
2019-05-20 08:55:19 +02:00
|
|
|
searchRow.appendChild(hideHeadCell);
|
2018-07-02 00:29:41 +02:00
|
|
|
|
|
|
|
const restHeadCell = document.createElement("td");
|
|
|
|
restHeadCell.colSpan = 2;
|
2018-09-18 13:14:39 +02:00
|
|
|
if (restContent){
|
|
|
|
restHeadCell.appendChild(restContent);
|
|
|
|
}
|
2019-05-20 08:55:19 +02:00
|
|
|
searchRow.appendChild(restHeadCell);
|
|
|
|
tHead.appendChild(searchRow);
|
|
|
|
|
|
|
|
const displayHiddenRow = document.createElement("tr");
|
|
|
|
displayHiddenRow.className = "settingRow displayHiddenRow";
|
|
|
|
displayHiddenRow.appendChild(hideHeadCell.cloneNode());
|
|
|
|
const displayHiddenDescription = document.createElement("td");
|
|
|
|
displayHiddenDescription.appendChild(createDescription(displayHidden));
|
|
|
|
displayHiddenRow.appendChild(displayHiddenDescription);
|
|
|
|
|
|
|
|
var displayHiddenInteraction = document.createElement("td");
|
|
|
|
displayHiddenInteraction.appendChild(createInteraction(displayHidden));
|
|
|
|
displayHiddenRow.appendChild(displayHiddenInteraction);
|
|
|
|
tHead.appendChild(displayHiddenRow);
|
2018-07-02 00:29:41 +02:00
|
|
|
return tHead;
|
|
|
|
}
|
|
|
|
|
|
|
|
scope.createThead = createThead;
|
2017-11-07 00:36:44 +01:00
|
|
|
}());
|