1
0
Fork 0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-07-04 04:26:35 +02:00

Create search in options page

Fixes #242
This commit is contained in:
kkapsner 2018-09-18 13:14:39 +02:00
parent feff447409
commit e3861e67b4
8 changed files with 166 additions and 3 deletions

51
lib/search.js Normal file
View file

@ -0,0 +1,51 @@
/* 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 {
window.scope.search = {};
scope = window.scope.search;
}
const texts = [];
scope.register = function(text, content){
texts.push({text: text.toLowerCase(), content});
};
scope.search = function(search){
search = search.toLowerCase();
const result = [];
texts.forEach(function(text){
if (text.text.indexOf(search) !== -1){
result.push(text.content);
}
});
return result;
};
const searchListeners = [];
scope.init = function(){
const node = document.createElement("input");
node.id = "search";
node.placeholder = browser.i18n.getMessage("search");
window.setTimeout(() => node.focus(), 1);
let lastResults = [];
node.addEventListener("input", function(){
const search = this.value;
const results = search? scope.search(search): [];
searchListeners.forEach(function(callback){
callback({search, results, lastResults});
});
lastResults = results;
});
return node;
};
scope.on = function(callback){
searchListeners.push(callback);
};
}());

53
lib/settingStrings.js Normal file
View file

@ -0,0 +1,53 @@
/* 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 {
scope = {};
window.scope.settingStrings = scope;
}
scope.getMessages = function(settingDefinition){
const messages = [
settingDefinition.name + "_title",
settingDefinition.name + "_description",
];
if (settingDefinition.urlSpecific){
messages.push(settingDefinition.name + "_urlSpecific");
}
if (settingDefinition.options){
settingDefinition.options.forEach(function(option){
messages.push(settingDefinition.name + "_option." + option);
});
}
return messages;
};
scope.getStrings = function(settingDefinition){
const strings = [];
function addString(string){
if ((typeof string) === "string" && string.trim()){
strings.push(string);
}
}
addString(settingDefinition.name);
if (settingDefinition.options){
settingDefinition.options.forEach(function(option){
addString(option);
});
}
scope.getMessages(settingDefinition).forEach(function(message){
addString(browser.i18n.getMessage(message));
});
return strings;
};
}());