web-api-manager/add-on/popup/js/popup.js

63 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-10-14 18:52:56 +00:00
/*jslint es6: true*/
/*global window*/
(function () {
2017-10-14 20:24:18 +00:00
"use strict";
const rootObject = window.browser || window.chrome;
2017-10-14 20:24:18 +00:00
const doc = window.document;
2017-10-14 20:24:18 +00:00
const configureButton = doc.getElementById("config-page-link");
const listGroupElm = doc.querySelector("ul.list-group");
const addRuleToList = function (hostToRuleMapping, listElm, aHostName) {
const domainRule = hostToRuleMapping[aHostName];
const liElm = doc.createElement("li");
liElm.className = "list-group-item";
if (domainRule !== "(default)") {
liElm.className += " list-group-item-success";
}
const spanElm = doc.createElement("span");
spanElm.className = "badge";
const badgeText = doc.createTextNode(domainRule);
spanElm.appendChild(badgeText);
liElm.appendChild(spanElm);
const textElm = doc.createTextNode(aHostName);
liElm.appendChild(textElm);
listElm.appendChild(liElm);
};
2017-10-14 18:52:56 +00:00
configureButton.addEventListener("click", function (event) {
rootObject.runtime.openOptionsPage();
event.preventDefault();
event.stopImmediatePropagation();
}, false);
rootObject.tabs.executeScript(
{
allFrames: true,
code: "window.location.host"
},
function (response) {
const uniqueDomains = Array.from(new Set(response)).sort();
const message = ["rulesForDomains", uniqueDomains];
rootObject.runtime.sendMessage(message, function (response) {
doc.body.className = "loaded";
2017-10-14 18:52:56 +00:00
const domainNames = Object.keys(response);
const addRuleToListBound = addRuleToList.bind(undefined, response, listGroupElm);
domainNames.forEach(addRuleToListBound);
});
}
);
}());