Skip to content

Commit

Permalink
* Rewrite Popup Generation Code
Browse files Browse the repository at this point in the history
* Pass a plain object to the popup instead of Ecma6 Map (for browsers, which stringify the messages passed around, like chrome)
  • Loading branch information
HostedDinner committed Feb 5, 2018
1 parent 6b63cd4 commit ce1715a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 37 deletions.
18 changes: 9 additions & 9 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let popupConnectionTabId = null;
// types

function TabStorage(){
this.hostnames = new Map();
this.hostnames = {};
this.main = new IpInfo('', '', false);
}

Expand Down Expand Up @@ -76,9 +76,9 @@ function queryActiveTabId(){
return new Promise((resolve, reject) => {
browser.tabs.query({active:true, currentWindow:true}, (tabs) => {
if(tabs.length === 1){
return resolve(tabs[0].id);
resolve(tabs[0].id);
}else{
return reject(null);
reject("Found " + tabs.length + " Tabs, instead of 1");
}
});
});
Expand All @@ -102,7 +102,7 @@ function updatePageAction(tabId){
// send Message to information popup (if its connected at the moment)
if(popupConnectionPort !== null && tabId === popupConnectionTabId)
popupConnectionPort.postMessage({action: 'updateContent', tabStorage});

// sets the PageAction title and icon accordingly
browser.pageAction.setTitle({
tabId,
Expand Down Expand Up @@ -192,13 +192,12 @@ browser.webRequest.onResponseStarted.addListener((details) => {
}


let ipsForHostname = tabStorage.hostnames.get(host);
let ipsForHostname = tabStorage.hostnames[host];
if(ipsForHostname === undefined){
ipsForHostname = new Map();
tabStorage.hostnames.set(host, ipsForHostname);
ipsForHostname = {};
}

let counterIpInfo = ipsForHostname.get(ip);
let counterIpInfo = ipsForHostname[ip];
if(counterIpInfo === undefined){
counterIpInfo = new CounterIpInfo(host, ip, isCached, isMain);
}else{
Expand All @@ -208,7 +207,8 @@ browser.webRequest.onResponseStarted.addListener((details) => {
}
//counterIpInfo.incrementCounter(isCached);
}
ipsForHostname.set(ip, counterIpInfo);
ipsForHostname[ip] = counterIpInfo;
tabStorage.hostnames[host] = ipsForHostname;

updatePageAction(tabId);

Expand Down
75 changes: 47 additions & 28 deletions src/popup/information.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,75 +4,94 @@ const ICONDIR = '../icons/';
/**
* Builds the table of elements
*
* @param {type} tableElement
* @param {HTMLTableElement} table
* @param {TabStorage} tabStorage
* @returns {Boolean}
*/
function buildTable(tableElement, tabStorage){
function buildTable(table, tabStorage){

let atLeastOne = false;

deleteAllRows(tableElement);
deleteAllRows(table);

for (var [hostname, ipsForHostname] of tabStorage.hostnames){
for(var [ip, counterIpInfo] of ipsForHostname){
buildRow(tableElement, counterIpInfo);
atLeastOne = true;
for(let hostnameProps in tabStorage.hostnames) {
if(tabStorage.hostnames.hasOwnProperty(hostnameProps)) {
let ipsForHostname = tabStorage.hostnames[hostnameProps];

for(var ipsProps in ipsForHostname) {
if(ipsForHostname.hasOwnProperty(ipsProps)){
let counterIpInfo = ipsForHostname[ipsProps];

let newRowElement = table.insertRow(-1);
buildRow(newRowElement, counterIpInfo);
atLeastOne = true;
}
}
}
}

return atLeastOne;
}

/**
* Builds one row
*
* @param {type} tableElement
* @param {HTMLTableRowElement} row
* @param {CounterIpInfo} counterIpInfo
* @returns {void}
*/
function buildRow(tableElement, counterIpInfo){

let row = tableElement.insertRow(-1);

row.insertCell(0).innerHTML = getIpVersionImageTag(counterIpInfo.ipVersion);
function buildRow(row, counterIpInfo){
row.insertCell(0).appendChild(getIpVersionImg(counterIpInfo.ipVersion));
row.insertCell(1).innerHTML = '(' + counterIpInfo.counter + ')';
row.insertCell(2).innerHTML = getHostNameTag(counterIpInfo.hostname, counterIpInfo.isMain);
row.insertCell(2).appendChild(getHostNameSpan(counterIpInfo.hostname, counterIpInfo.isMain));
row.insertCell(3).innerHTML = counterIpInfo.ip;
}

/**
* Constructs the <img> tag
* Constructs the <img> element
*
* @param {String} ipVersion
* @returns {String}
* @returns {HTMLElement}
*/
function getIpVersionImageTag(ipVersion){

function getIpVersionImg(ipVersion){
let newImageHTMLElement = document.createElement("img");
let pathSVG = [ICONDIR, ipVersion, '.svg'].join('');

return '<img src="' + pathSVG + '" width="18" height="18">';
newImageHTMLElement.src = pathSVG;
newImageHTMLElement.width = 18;
newImageHTMLElement.height = 18;

return newImageHTMLElement;
}

function getHostNameTag(hostName, isMain){
/*
* Constructs the hostname element
*
* @param {String} hostName
* @param {Boolean} isMain
* @returns {HTMLElement}
*/
function getHostNameSpan(hostName, isMain){
let newSpanHTMLElement = document.createElement("span");

newSpanHTMLElement.innerHTML = hostName;
if(isMain)
return ['<span class="mainItem">', hostName, '</span>'].join('');
else
return hostName;
newSpanHTMLElement.className = 'mainItem';

return newSpanHTMLElement;
}



/**
* Deletes all rows from the table
*
* @param {type} tableElement
* @param {HTMLTableElement } table
* @returns {void}
*/
function deleteAllRows(tableElement) {
let rowCount = tableElement.rows.length;
function deleteAllRows(table) {
let rowCount = table.rows.length;
for (let i = rowCount - 1; i >= 0; i--) {
tableElement.deleteRow(i);
table.deleteRow(i);
}
}

Expand Down

0 comments on commit ce1715a

Please sign in to comment.