Advertisement
rg443

Untitled

Sep 24th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. https://extension.dataprovider.com/api/eii-trustable/?api_key=0914bd73937bcfdaa40ce19a3bc7417e&name=www.cisco.com
  2. api_key: 0914bd73937bcfdaa40ce19a3bc7417e
  3. {"total":1,"offset":0,"limit":100,"data":[{"eii":79,"trustable":true}],"cache":true}
  4.  
  5. <input type="hidden" id="api_key" value="0914bd73937bcfdaa40ce19a3bc7417e">
  6.  
  7.  
  8. // clear settings every ## debug
  9. const init_settings = {
  10.     api_key : null,
  11.     inbox : false,
  12.     activated : false,
  13.     fp: null
  14. };
  15.  
  16. //let base_url = "https://plugin.dataprovider.com/";
  17. let base_url = "https://extension.dataprovider.com/";
  18. // let base_url = "https://localhost/plugin.dp.local/web/index.php/";
  19.  
  20.  
  21. function processFingerprint(fp) {
  22.     chrome.storage.local.get('settings', function(data) {
  23.         if (data.hasOwnProperty('settings')) {
  24.             const settings = data.settings;
  25.             settings.fp = fp;
  26.             saveSettings(settings);
  27.         }
  28.     });
  29. }
  30.  
  31. /**
  32.  *
  33.  */
  34. function checkActivation(){
  35.     chrome.storage.local.get('settings', function(data){
  36.         if(data.hasOwnProperty('settings')) {
  37.             const settings = data.settings;
  38.             if(typeof settings != 'undefined' && settings.api_key != null){}
  39.         }else{
  40.             // initialize settings
  41.             saveSettings(init_settings);
  42.         }
  43.     });
  44. }
  45.  
  46. /**
  47.  * Save settings for user
  48.  * @param settings
  49.  */
  50. function saveSettings(settings){
  51.     chrome.storage.local.get('settings', function (stored_settings) {
  52.         const settings_unchanged = (JSON.stringify(stored_settings.settings) === JSON.stringify(settings));
  53.         if (!settings_unchanged) {
  54.             chrome.storage.local.set({'settings': settings}, function () {});
  55.         }
  56.     });
  57. }
  58.  
  59. /**
  60.  * Extracts url from current tab
  61.  * @param url
  62.  * @returns {*} match or false
  63.  */
  64. function getHostname(url){
  65.     const match = url.match(/^[\w-]+:\/{2,}\[?([\w\.:-]+)\]?(?::[0-9]*)?/);
  66.     if(match !== null){
  67.         if(match[1] == "localhost"){
  68.             return false;
  69.         }else{
  70.             return match[1];
  71.         }
  72.     }else{
  73.         return false; // not a valid url or local
  74.     }
  75. }
  76.  
  77. /**
  78.  * Checks protocol of given url only returns true when protocol is http/https
  79.  * @param url input url with protocol
  80.  * @returns {boolean} true when http/https
  81.  */
  82. function checkProtocol(url){
  83.     const protocol = url.substring(0,(url.indexOf("//")-1));
  84.     // return true or false based on protocol
  85.     return (protocol == "http" || protocol == "https");
  86. }
  87.  
  88. function updatePluginThumbnail(hostname){
  89.     chrome.storage.local.get('settings', function(data){
  90.         if(data.settings.api_key != 'undefined' && data.settings.api_key != null){
  91.             //var lookup_url = ;
  92.             fetch(base_url+"api/eii-trustable/?api_key="+data.settings.api_key+"&name="+hostname,
  93.                 {method: 'get'})
  94.                 .then(handleErrors)
  95.                 .then(parseJSON)
  96.                 .then(function(json){
  97.                     console.log('json sucks');
  98.                     if (typeof json.error == 'undefined' && Object.prototype.toString.call(json.data) == '[object Array]') {
  99.                         console.log(json.data[0]);
  100.                         chrome.storage.local.get({'eii_cache': {}}, function(storage) {
  101.                             /*
  102.                                 This now also contains boolean that says if website is trustable or not
  103.                             */
  104.                             if (typeof json.data[0].trustable == 'undefined') {
  105.                                 json.data[0].trustable = true;
  106.                             }
  107.                             storage.eii_cache[hostname] = json.data[0];
  108.  
  109.  
  110.                             chrome.storage.local.set(storage);
  111.                         });
  112.                         // no errors
  113.                         updateThumb(json.data[0]);
  114.                     }else{
  115.                         chrome.browserAction.setIcon({path:"img/icon/icon_original.png"});
  116.                     }
  117.                 }).catch(function(error){
  118.                     chrome.browserAction.setIcon({path:"img/icon/icon_original.png"});
  119.                 });
  120.         }
  121.     });
  122. };
  123.  
  124. function getDataproviderPopupData(hostname){
  125.     return new Promise(function(resolve, reject) {
  126.         chrome.storage.local.get('settings', function(data){
  127.             const api_key = data.settings.api_key;
  128.             const dp_url = base_url+"api/popup/?api_key="+api_key+"&name="+hostname;
  129.  
  130.             const request = new XMLHttpRequest();
  131.             request.open('GET', dp_url, true);
  132.             request.onreadystatechange = function() {
  133.                 // request finished and response is ready.
  134.                 if (request.readyState == 4 && request.status == 200) {
  135.                     resolve(request.responseText);
  136.                 }else if(request.status == 404){
  137.                     resolve(false);
  138.                 }
  139.             };
  140.  
  141.             request.onerror = function(){
  142.                 reject({
  143.                     status: this.status,
  144.                     statusText: request.statusText
  145.                 });
  146.             };
  147.  
  148.             request.send();
  149.         });
  150.     });
  151. }
  152.  
  153. function getParameterByName(queryString, name) {
  154.     // Escape special RegExp characters
  155.     name = name.replace(/[[^$.|?*+(){}\\]/g, '\\$&');
  156.     // Create Regular expression
  157.     const regex = new RegExp("(?:[?&]|^)" + name + "=([^&#]*)");
  158.     // Attempt to get a match
  159.     const results = regex.exec(queryString);
  160.     if(results != null){
  161.         return decodeURIComponent(results[1].replace(/\+/g, " ")) || '';
  162.     }else{
  163.         return false;
  164.     }
  165. }
  166.  
  167. /**
  168.  * Update
  169.  * @param data
  170.  */
  171. function updateThumb(data) {
  172.     if(typeof data.eii == 'undefined' ){
  173.         console.log("eii: " + data.eii);
  174.         chrome.browserAction.setIcon({path:"img/icon/icon_original.png"});
  175.     }else{
  176.         console.log("eii: " + data.eii);
  177.         if (data.eii > 60) {
  178.             chrome.browserAction.setIcon({path:"img/icon/icon_green.png"});
  179.         } else if (data.eii < 30) {
  180.             chrome.browserAction.setIcon({path:"img/icon/icon_red.png"});
  181.         } else {
  182.             chrome.browserAction.setIcon({path:"img/icon/icon_orange.png"});
  183.         }
  184.     }
  185. }
  186.  
  187.  
  188. /**
  189.  * prechecks the statuscode
  190.  * @param response
  191.  * @returns {*}
  192.  */
  193. function handleErrors(response) {
  194.     if (response.status !== 200)
  195.         return;
  196.     return response;
  197. }
  198.  
  199. function parseJSON(response){
  200.     if(!!response){
  201.         // return tryParseJSON(response);
  202.         return response.json();
  203.     }else{
  204.         return false;
  205.     }
  206. }
  207.  
  208.  
  209.  
  210.  
  211.  
  212. /*
  213.  ** file: js/background.js
  214.  ** description: javascript code for retrieving Dataprovider API data.
  215.  */
  216.  
  217. chrome.contextMenus.onClicked.addListener(contextHandler);
  218.  
  219.  
  220. function contextHandler(info, tab) {
  221.    
  222.     if (info.menuItemId == "clear-history") {
  223.         chrome.storage.local.remove("dontShowArray", function () {console.log("cleared trustscore saved hidden websites")});
  224.     }
  225.    
  226.     if (info.menuItemId == "clear-history-cur") {
  227.         chrome.storage.local.get("dontShowArray", function(result) {
  228.             dontShowArray = result["dontShowArray"];
  229.             if (typeof dontShowArray != "undefined") {
  230.                 chrome.tabs.query({'active': true}, function (tabs) {
  231.                     var url = tabs[0].url;
  232.                     var index = dontShowArray.indexOf(getHostname(url));
  233.                     if (index > -1) {
  234.                         dontShowArray.splice(index, 1);
  235.                         chrome.storage.local.set({dontShowArray:dontShowArray});
  236.                     }
  237.                 });
  238.             }
  239.         });
  240.     }
  241.  
  242. }
  243.  
  244. chrome.runtime.onInstalled.addListener(function(details){
  245.     chrome.storage.local.get('settings', function(data){
  246.         if(details.reason == "install"){
  247.             if(data.hasOwnProperty('settings')){
  248.                 checkActivation();
  249.                 //initSearchEngineConfig();
  250.             }else{
  251.                 saveSettings(init_settings);
  252.                 //initSearchEngineConfig();
  253.             }
  254.  
  255.             chrome.tabs.create({url : base_url+"activate/welcome"});
  256.         }else if(details.reason == "update"){
  257.             const thisVersion = chrome.runtime.getManifest().version;
  258.             console.log("Updated from " + details.previousVersion + " to " + thisVersion + "!");
  259.  
  260.             // clear cache
  261.             chrome.storage.local.set({'eii_cache': {}});
  262.             chrome.storage.local.set({'openTabs': {}});
  263.  
  264.             if (data.hasOwnProperty('settings')) {
  265.                 const settings = data.settings;
  266.  
  267.                 if(!settings.activated){
  268.                     // if not activated yet and has no api key, request key
  269.                     if(settings.api_key){
  270.                         // if not activated, but has api key,
  271.                         console.log("not activated + has api_key");
  272.                         activateExtBackground();
  273.                     }else if(settings.api_key){}
  274.                     activateExtBackground();
  275.                 }
  276.             }else{
  277.                 saveSettings(init_settings);
  278.                 activateExtBackground();
  279.             }
  280.         }
  281.     });
  282.     chrome.contextMenus.create({"title":"Clear trust score history", "id":"clear-history", "contexts":["browser_action"]});
  283.     chrome.contextMenus.create({"title":"Clear trust score for this site", "id":"clear-history-cur", "contexts":["browser_action"]});
  284. });
  285.  
  286. function activateExtBackground(){
  287.  
  288.     new Fingerprint2().get(function(result){
  289.         processFingerprint(result);
  290.     });
  291.  
  292.     fetch(base_url+"activate/welcome", {method: 'get'})
  293.         .then(function(request){
  294.             if(request.redirected){
  295.                 const queryString = /\?[^#]+(?=#|$)|$/.exec(request.url)[0];
  296.                 const api_key = getParameterByName(queryString, 'api_key');
  297.                 if(api_key){
  298.                     chrome.storage.local.get('settings', function(data){
  299.                         if(data.hasOwnProperty('settings')) {
  300.                             const settings = data.settings;
  301.                             settings.api_key = api_key;
  302.                             settings.activated = true;
  303.                             saveSettings(settings);
  304.                         }
  305.                     });
  306.                 }
  307.             }
  308.         }).catch(function(error){
  309.             console.log(error);
  310.         });
  311. }
  312.  
  313. /**
  314.  * check if url for tab hasn't changed
  315.  */
  316. chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, updatedTab) {
  317.     if (changeInfo.status == 'loading') {
  318.         chrome.storage.local.get({'openTabs': {}}, function(storage) {
  319.             let prevUrl = "";
  320.             const currUrl = getHostname(updatedTab.url);
  321.  
  322.             if(tabId in storage.openTabs){
  323.                 prevUrl = storage.openTabs[tabId];
  324.             }
  325.  
  326.             if(updatedTab.url.indexOf(base_url+"activate") > -1){
  327.                 activateExtension(updatedTab.url);
  328.             }
  329.  
  330.             if(prevUrl !== currUrl){
  331.                 retrieveIconDataFromCache(updatedTab);
  332.             }
  333.  
  334.             storage.openTabs[tabId] = currUrl;
  335.             chrome.storage.local.set(storage);
  336.         });
  337.     }
  338. });
  339.  
  340. chrome.tabs.onActivated.addListener(function(activeInfo) {
  341.     chrome.tabs.get(activeInfo.tabId, function(activeTab){
  342.  
  343.         if (chrome.runtime.lastError) {
  344.             console.log(chrome.runtime.lastError.message);
  345.         }else{
  346.  
  347.             chrome.storage.local.get({'openTabs': {}}, function(storage) {
  348.                 const currUrl = getHostname(activeTab.url);
  349.  
  350.                 if(activeTab.url.indexOf(base_url+"activate") > -1){
  351.                     activateExtension(activeTab.url);
  352.                 }
  353.  
  354.                 retrieveIconDataFromCache(activeTab);
  355.  
  356.                 storage.openTabs[activeInfo.tabId] = currUrl;
  357.                 chrome.storage.local.set(storage);
  358.             });
  359.  
  360.         }
  361.     });
  362. });
  363.  
  364. function retrieveIconData(tab){
  365.     chrome.browserAction.setIcon({path:"../img/icon/icon_original.png"});
  366.  
  367.     if(typeof tab !='undefined' && typeof tab.url !='undefined' ){
  368.         if(checkProtocol(tab.url)){
  369.             const hostname = getHostname(tab.url);
  370.             if (hostname){
  371.                 updatePluginThumbnail(hostname);
  372.             }
  373.         }
  374.     }
  375. }
  376.  
  377. function retrieveIconDataFromCache(tab) {
  378.     chrome.storage.local.get({'eii_cache' : {}}, function(storage){
  379.         if(typeof tab !='undefined' && typeof tab.url !='undefined' && checkProtocol(tab.url)) {
  380.             const hostname = getHostname(tab.url);
  381.  
  382.             if(typeof storage.eii_cache != 'undefined'){
  383.                 if(typeof storage.eii_cache[hostname] != 'undefined') {
  384.                     updateThumb(storage.eii_cache[hostname]);
  385.                 }else{
  386.                     retrieveIconData(tab);
  387.                 }
  388.             }else{
  389.                 retrieveIconData(tab);
  390.             }
  391.         }
  392.     });
  393. }
  394.  
  395.  
  396. function activateExtension(url){
  397.     const queryString = /\?[^#]+(?=#|$)|$/.exec(url)[0];
  398.     const api_key = getParameterByName(queryString, 'api_key');
  399.     fetch(base_url+"api/check-activation/?api_key="+ api_key, {method: 'get'})
  400.         .then(handleErrors)
  401.         .then(parseJSON)
  402.         .then(function(json){
  403.             if (typeof json.activated != 'undefined') {
  404.                 chrome.storage.local.get('settings', function(data){
  405.                     if(data.hasOwnProperty('settings')) {
  406.                         const settings = data.settings;
  407.                         settings.api_key = api_key;
  408.                         settings.activated = json.activated;
  409.                         saveSettings(settings);
  410.                     }
  411.                 });
  412.             }
  413.         }).catch(function(error){
  414.             console.log(error);
  415.         });
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement