mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
* Replace var with let in examples store-collected-images/webextension-plain/deps/uuidv4.js * Reverted third–party code
45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
// Get the saved stats and render the data in the popup window.
|
|
const MAX_ITEMS = 5;
|
|
|
|
function sorter(array) {
|
|
return Object.keys(array).sort((a, b) => {
|
|
return array[a] <= array[b];
|
|
});
|
|
}
|
|
|
|
function addElements(element, array, callback) {
|
|
while(element.firstChild) {
|
|
element.removeChild(element.firstChild);
|
|
}
|
|
|
|
for (let i=0; i < array.length; i++) {
|
|
if (i >= MAX_ITEMS) {
|
|
break;
|
|
}
|
|
|
|
const listItem = document.createElement("li");
|
|
listItem.textContent = callback(array[i]);
|
|
element.appendChild(listItem);
|
|
}
|
|
}
|
|
|
|
let gettingStoredStats = browser.storage.local.get();
|
|
gettingStoredStats.then(results => {
|
|
if (results.type.length === 0 || results.host.length === 0) {
|
|
return;
|
|
}
|
|
|
|
let hostElement = document.getElementById("hosts");
|
|
let sortedHosts = sorter(results.host);
|
|
addElements(hostElement, sortedHosts, (host) => {
|
|
return `${host}: ${results.host[host]} visit(s)`;
|
|
});
|
|
|
|
let typeElement = document.getElementById("types");
|
|
let sortedTypes = sorter(results.type);
|
|
addElements(typeElement, sortedTypes, (type) => {
|
|
return `${type}: ${results.type[type]} use(s)`;
|
|
});
|
|
|
|
});
|