From fb35bc72eb77ffd462a2d9b90cc5f9a18f8d1700 Mon Sep 17 00:00:00 2001 From: NateN1222 Date: Wed, 2 Aug 2017 13:29:13 -0500 Subject: Rewrote main_panel.js, added debugging tools, started implementing the blacklist/whitelist system --- html/display_panel/content/main_panel.js | 168 +++++++++++++++++++++---------- 1 file changed, 116 insertions(+), 52 deletions(-) (limited to 'html/display_panel/content/main_panel.js') diff --git a/html/display_panel/content/main_panel.js b/html/display_panel/content/main_panel.js index a724c75..880d963 100644 --- a/html/display_panel/content/main_panel.js +++ b/html/display_panel/content/main_panel.js @@ -22,72 +22,126 @@ set_webex(); var myPort = webex.runtime.connect({name:"port-from-cs"}); var current_blocked_data; + + + +/* +* Makes a button appear that calls a function when you press it. +* +* I copied and pasted this from something else I wrote. It's quite useful. +* +*/ +var button_i = 0; +function new_debug_button(name_text,callback){ + if(document.getElementById("abc123_main_div") === null){ + var to_insert = '
'; + document.body.insertAdjacentHTML('afterbegin', to_insert); + } + + var button_html = '
'; + + document.getElementById("abc123_main_div").insertAdjacentHTML('afterbegin', button_html); + document.getElementById("abc123_button_"+button_i).addEventListener("click",callback); + button_i = button_i + 1; +} + + + + + + +/* +* Takes in the script data and color of h2 element +* Writes to category specified by "name" as used in HTML +* (name will probably either be "blacklisted", "whitelisted", "accepted", or "blocked") +*/ +function write_elements(data,name,color){ + var url = data["url"]; + var button_html = '
'; + var button_html_2 = '
'; + var button_html_3 = '
'; + var heading = document.getElementById(name).getElementsByTagName("h2")[0]; + var list = document.getElementById(name).getElementsByTagName("ul")[0]; + if(data[name].length == 0){ + // default message + list.innerHTML = "
  • No "+ name +" scripts on this page.
  • " + } else{ + heading.innerHTML = "

    List of
    " + name.toUpperCase() + "
    javascript in " + data["url"]+":

    "; + } + // Iterate over data[name] and generate bulleted list + for(var i = 0; i < data[name].length; i++){ + list.innerHTML += "
  • "+data[name][i][0]+ ":
    " + data[name][i][1]+"\n"+button_html+"

    \n"+button_html_2+"

    \n"+button_html_3+"
  • "; + document.getElementById("temp").id = name+"_"+i; + document.getElementById("temp2").id = name+"_2_"+i; + document.getElementById("temp3").id = name+"_3_"+i; + } + if(data[name].length != 0){ + // add click listeners to the buttons + for(var i = 0; i < data[name].length; i++){ + // Make sure this causes generate_html to get called again with updated data + document.getElementById(name+"_"+i).addEventListener("click",function(info){ + var temp = current_blocked_data[name][parseInt(info.path[0].id.match(/\d/g)[0])]; + console.log("Moving script " + temp[0] + " to blacklist"); + var script_name = this.parentElement.parentElement.parentElement.parentElement.id; + myPort.postMessage({"blacklist": temp}); + }); + document.getElementById(name+"_2_"+i).addEventListener("click",function(info){ + var temp = current_blocked_data[name][parseInt(info.path[0].id.match(/\d/g)[1])]; + console.log("Moving script " + temp[0] + " to whitelist"); + var script_name = this.parentElement.parentElement.parentElement.parentElement.id; + myPort.postMessage({"whitelist": temp}); + }); + + document.getElementById(name+"_3_"+i).addEventListener("click",function(info){ + var temp = current_blocked_data[name][parseInt(info.path[0].id.match(/\d/g)[1])]; + console.log("Forget preferences for script " + temp[0]); + var script_name = this.parentElement.parentElement.parentElement.parentElement.id; + myPort.postMessage({"forget": temp}); + }); + } + } + +} + + + /** * update the HTML of the pop-up window. * If return_HTML is true, it returns the HTML of the popup window without updating it. * example input: * * var example_input = { -* "accepted": [["REASON 1","SOURCE 1"],["REASON 2","SOURCE 2"]], -* "blocked": [["REASON 1","SOURCE 1"],["REASON 2","SOURCE 2"]], -* "url": "example.com" -* } +* "accepted": [["FILENAME 1","REASON 1"],["FILENAME 2","REASON 2"]], +* "blocked": [["FILENAME 1","REASON 1"],["FILENAME 2","REASON 2"]], +* "whitelisted": [["FILENAME 1","REASON 1"],["FILENAME 2","REASON 2"]], +* "blacklisted": [["FILENAME 1","REASON 1"],["FILENAME 2","REASON 2"]], +* "url":"example.com" +* }; * */ function generate_HTML(blocked_data){ - - current_blocked_data = blocked_data; - var a = blocked_data; + current_blocked_data = blocked_data;//unused? + + // This should send a message to invoke the content finder var button_complain = 'Complain to site owner'; + // This should update the persistent options var button_allow_all = 'Allow all scripts in this page'; + // This will call "Forget preferences" on every script. var button_block_nonfree = 'Block all nonfree/nontrivial scripts from this page'; + // This should send a message that calls "open_popup_tab()" in the background script var button_new_tab = 'Open this report in a new tab'; - var button_whitelist = '
    '; - var htmlDoc = document;// Not neccessary - var accept = htmlDoc.getElementById("accepted"); - var blocked = htmlDoc.getElementById("blocked"); - // HTML taken directly from the current LibreJS, display-panel.html - if(a["accepted"].length == 0){ - accept.innerHTML = '
  • LibreJS did not allow the execution of any scripts on this page: \n\n
  • '; - } else{ - accept.innerHTML = ""; - accept.innerHTML = '

    List of
    ACCEPTED
    javascript in '+a["url"]+':

    '; - accept.innerHTML += '"; - } - // HTML taken directly from the current LibreJS, display-panel.html - if(a["blocked"].length == 0){ - blocked.innerHTML += '
  • LibreJS did not block any scripts on this page: \n\n
  • '; - } else{ - blocked.innerHTML = ""; - blocked.innerHTML = "

    List of
    BLOCKED
    javascript in" + a["url"]+":

    "; - blocked.innerHTML += '"; - // add click listeners to the buttons - for(var i = 0; i < a["blocked"].length; i++){ - document.getElementById("wl_"+i).addEventListener("click",function(a){ - console.log(a.path[0].id + " clicked"); - var temp = current_blocked_data["blocked"][parseInt(a.path[0].id.substr(3))]; - console.log(temp); - myPort.postMessage({"whitelist_script": temp}); - }); - } + + var to_clr = document.getElementsByClassName("blocked-js"); + + for(var i = 0; i < to_clr.length; i++){ + to_clr[i].innerHTML = ""; } - // At this point, it has the HTML that the popup needs and the only problem is - // getting it into the popup. (browserAction() needs a (local) URL to work). + + + write_elements(blocked_data,"accepted","green"); + write_elements(blocked_data,"whitelisted","green"); + write_elements(blocked_data,"blocked","red"); + write_elements(blocked_data,"blacklisted","red"); } @@ -96,9 +150,19 @@ myPort.onMessage.addListener(function(m) { generate_HTML(m["show_info"]); } }); +// Sends a message that tells the background script the window is open function onGot(tabInfo) { myPort.postMessage({"tab_info": tabInfo}); } var gettingCurrent = webex.tabs.getCurrent(onGot); + + + + +function print_local_storage(){ + myPort.postMessage({"printlocalstorage": true}); +} + +new_debug_button("Print local storage",print_local_storage); -- cgit v1.2.3