Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://extension.dataprovider.com/api/eii-trustable/?api_key=0914bd73937bcfdaa40ce19a3bc7417e&name=www.cisco.com
- api_key: 0914bd73937bcfdaa40ce19a3bc7417e
- {"total":1,"offset":0,"limit":100,"data":[{"eii":79,"trustable":true}],"cache":true}
- <input type="hidden" id="api_key" value="0914bd73937bcfdaa40ce19a3bc7417e">
- // clear settings every ## debug
- const init_settings = {
- api_key : null,
- inbox : false,
- activated : false,
- fp: null
- };
- //let base_url = "https://plugin.dataprovider.com/";
- let base_url = "https://extension.dataprovider.com/";
- // let base_url = "https://localhost/plugin.dp.local/web/index.php/";
- function processFingerprint(fp) {
- chrome.storage.local.get('settings', function(data) {
- if (data.hasOwnProperty('settings')) {
- const settings = data.settings;
- settings.fp = fp;
- saveSettings(settings);
- }
- });
- }
- /**
- *
- */
- function checkActivation(){
- chrome.storage.local.get('settings', function(data){
- if(data.hasOwnProperty('settings')) {
- const settings = data.settings;
- if(typeof settings != 'undefined' && settings.api_key != null){}
- }else{
- // initialize settings
- saveSettings(init_settings);
- }
- });
- }
- /**
- * Save settings for user
- * @param settings
- */
- function saveSettings(settings){
- chrome.storage.local.get('settings', function (stored_settings) {
- const settings_unchanged = (JSON.stringify(stored_settings.settings) === JSON.stringify(settings));
- if (!settings_unchanged) {
- chrome.storage.local.set({'settings': settings}, function () {});
- }
- });
- }
- /**
- * Extracts url from current tab
- * @param url
- * @returns {*} match or false
- */
- function getHostname(url){
- const match = url.match(/^[\w-]+:\/{2,}\[?([\w\.:-]+)\]?(?::[0-9]*)?/);
- if(match !== null){
- if(match[1] == "localhost"){
- return false;
- }else{
- return match[1];
- }
- }else{
- return false; // not a valid url or local
- }
- }
- /**
- * Checks protocol of given url only returns true when protocol is http/https
- * @param url input url with protocol
- * @returns {boolean} true when http/https
- */
- function checkProtocol(url){
- const protocol = url.substring(0,(url.indexOf("//")-1));
- // return true or false based on protocol
- return (protocol == "http" || protocol == "https");
- }
- function updatePluginThumbnail(hostname){
- chrome.storage.local.get('settings', function(data){
- if(data.settings.api_key != 'undefined' && data.settings.api_key != null){
- //var lookup_url = ;
- fetch(base_url+"api/eii-trustable/?api_key="+data.settings.api_key+"&name="+hostname,
- {method: 'get'})
- .then(handleErrors)
- .then(parseJSON)
- .then(function(json){
- console.log('json sucks');
- if (typeof json.error == 'undefined' && Object.prototype.toString.call(json.data) == '[object Array]') {
- console.log(json.data[0]);
- chrome.storage.local.get({'eii_cache': {}}, function(storage) {
- /*
- This now also contains boolean that says if website is trustable or not
- */
- if (typeof json.data[0].trustable == 'undefined') {
- json.data[0].trustable = true;
- }
- storage.eii_cache[hostname] = json.data[0];
- chrome.storage.local.set(storage);
- });
- // no errors
- updateThumb(json.data[0]);
- }else{
- chrome.browserAction.setIcon({path:"img/icon/icon_original.png"});
- }
- }).catch(function(error){
- chrome.browserAction.setIcon({path:"img/icon/icon_original.png"});
- });
- }
- });
- };
- function getDataproviderPopupData(hostname){
- return new Promise(function(resolve, reject) {
- chrome.storage.local.get('settings', function(data){
- const api_key = data.settings.api_key;
- const dp_url = base_url+"api/popup/?api_key="+api_key+"&name="+hostname;
- const request = new XMLHttpRequest();
- request.open('GET', dp_url, true);
- request.onreadystatechange = function() {
- // request finished and response is ready.
- if (request.readyState == 4 && request.status == 200) {
- resolve(request.responseText);
- }else if(request.status == 404){
- resolve(false);
- }
- };
- request.onerror = function(){
- reject({
- status: this.status,
- statusText: request.statusText
- });
- };
- request.send();
- });
- });
- }
- function getParameterByName(queryString, name) {
- // Escape special RegExp characters
- name = name.replace(/[[^$.|?*+(){}\\]/g, '\\$&');
- // Create Regular expression
- const regex = new RegExp("(?:[?&]|^)" + name + "=([^&#]*)");
- // Attempt to get a match
- const results = regex.exec(queryString);
- if(results != null){
- return decodeURIComponent(results[1].replace(/\+/g, " ")) || '';
- }else{
- return false;
- }
- }
- /**
- * Update
- * @param data
- */
- function updateThumb(data) {
- if(typeof data.eii == 'undefined' ){
- console.log("eii: " + data.eii);
- chrome.browserAction.setIcon({path:"img/icon/icon_original.png"});
- }else{
- console.log("eii: " + data.eii);
- if (data.eii > 60) {
- chrome.browserAction.setIcon({path:"img/icon/icon_green.png"});
- } else if (data.eii < 30) {
- chrome.browserAction.setIcon({path:"img/icon/icon_red.png"});
- } else {
- chrome.browserAction.setIcon({path:"img/icon/icon_orange.png"});
- }
- }
- }
- /**
- * prechecks the statuscode
- * @param response
- * @returns {*}
- */
- function handleErrors(response) {
- if (response.status !== 200)
- return;
- return response;
- }
- function parseJSON(response){
- if(!!response){
- // return tryParseJSON(response);
- return response.json();
- }else{
- return false;
- }
- }
- /*
- ** file: js/background.js
- ** description: javascript code for retrieving Dataprovider API data.
- */
- chrome.contextMenus.onClicked.addListener(contextHandler);
- function contextHandler(info, tab) {
- if (info.menuItemId == "clear-history") {
- chrome.storage.local.remove("dontShowArray", function () {console.log("cleared trustscore saved hidden websites")});
- }
- if (info.menuItemId == "clear-history-cur") {
- chrome.storage.local.get("dontShowArray", function(result) {
- dontShowArray = result["dontShowArray"];
- if (typeof dontShowArray != "undefined") {
- chrome.tabs.query({'active': true}, function (tabs) {
- var url = tabs[0].url;
- var index = dontShowArray.indexOf(getHostname(url));
- if (index > -1) {
- dontShowArray.splice(index, 1);
- chrome.storage.local.set({dontShowArray:dontShowArray});
- }
- });
- }
- });
- }
- }
- chrome.runtime.onInstalled.addListener(function(details){
- chrome.storage.local.get('settings', function(data){
- if(details.reason == "install"){
- if(data.hasOwnProperty('settings')){
- checkActivation();
- //initSearchEngineConfig();
- }else{
- saveSettings(init_settings);
- //initSearchEngineConfig();
- }
- chrome.tabs.create({url : base_url+"activate/welcome"});
- }else if(details.reason == "update"){
- const thisVersion = chrome.runtime.getManifest().version;
- console.log("Updated from " + details.previousVersion + " to " + thisVersion + "!");
- // clear cache
- chrome.storage.local.set({'eii_cache': {}});
- chrome.storage.local.set({'openTabs': {}});
- if (data.hasOwnProperty('settings')) {
- const settings = data.settings;
- if(!settings.activated){
- // if not activated yet and has no api key, request key
- if(settings.api_key){
- // if not activated, but has api key,
- console.log("not activated + has api_key");
- activateExtBackground();
- }else if(settings.api_key){}
- activateExtBackground();
- }
- }else{
- saveSettings(init_settings);
- activateExtBackground();
- }
- }
- });
- chrome.contextMenus.create({"title":"Clear trust score history", "id":"clear-history", "contexts":["browser_action"]});
- chrome.contextMenus.create({"title":"Clear trust score for this site", "id":"clear-history-cur", "contexts":["browser_action"]});
- });
- function activateExtBackground(){
- new Fingerprint2().get(function(result){
- processFingerprint(result);
- });
- fetch(base_url+"activate/welcome", {method: 'get'})
- .then(function(request){
- if(request.redirected){
- const queryString = /\?[^#]+(?=#|$)|$/.exec(request.url)[0];
- const api_key = getParameterByName(queryString, 'api_key');
- if(api_key){
- chrome.storage.local.get('settings', function(data){
- if(data.hasOwnProperty('settings')) {
- const settings = data.settings;
- settings.api_key = api_key;
- settings.activated = true;
- saveSettings(settings);
- }
- });
- }
- }
- }).catch(function(error){
- console.log(error);
- });
- }
- /**
- * check if url for tab hasn't changed
- */
- chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, updatedTab) {
- if (changeInfo.status == 'loading') {
- chrome.storage.local.get({'openTabs': {}}, function(storage) {
- let prevUrl = "";
- const currUrl = getHostname(updatedTab.url);
- if(tabId in storage.openTabs){
- prevUrl = storage.openTabs[tabId];
- }
- if(updatedTab.url.indexOf(base_url+"activate") > -1){
- activateExtension(updatedTab.url);
- }
- if(prevUrl !== currUrl){
- retrieveIconDataFromCache(updatedTab);
- }
- storage.openTabs[tabId] = currUrl;
- chrome.storage.local.set(storage);
- });
- }
- });
- chrome.tabs.onActivated.addListener(function(activeInfo) {
- chrome.tabs.get(activeInfo.tabId, function(activeTab){
- if (chrome.runtime.lastError) {
- console.log(chrome.runtime.lastError.message);
- }else{
- chrome.storage.local.get({'openTabs': {}}, function(storage) {
- const currUrl = getHostname(activeTab.url);
- if(activeTab.url.indexOf(base_url+"activate") > -1){
- activateExtension(activeTab.url);
- }
- retrieveIconDataFromCache(activeTab);
- storage.openTabs[activeInfo.tabId] = currUrl;
- chrome.storage.local.set(storage);
- });
- }
- });
- });
- function retrieveIconData(tab){
- chrome.browserAction.setIcon({path:"../img/icon/icon_original.png"});
- if(typeof tab !='undefined' && typeof tab.url !='undefined' ){
- if(checkProtocol(tab.url)){
- const hostname = getHostname(tab.url);
- if (hostname){
- updatePluginThumbnail(hostname);
- }
- }
- }
- }
- function retrieveIconDataFromCache(tab) {
- chrome.storage.local.get({'eii_cache' : {}}, function(storage){
- if(typeof tab !='undefined' && typeof tab.url !='undefined' && checkProtocol(tab.url)) {
- const hostname = getHostname(tab.url);
- if(typeof storage.eii_cache != 'undefined'){
- if(typeof storage.eii_cache[hostname] != 'undefined') {
- updateThumb(storage.eii_cache[hostname]);
- }else{
- retrieveIconData(tab);
- }
- }else{
- retrieveIconData(tab);
- }
- }
- });
- }
- function activateExtension(url){
- const queryString = /\?[^#]+(?=#|$)|$/.exec(url)[0];
- const api_key = getParameterByName(queryString, 'api_key');
- fetch(base_url+"api/check-activation/?api_key="+ api_key, {method: 'get'})
- .then(handleErrors)
- .then(parseJSON)
- .then(function(json){
- if (typeof json.activated != 'undefined') {
- chrome.storage.local.get('settings', function(data){
- if(data.hasOwnProperty('settings')) {
- const settings = data.settings;
- settings.api_key = api_key;
- settings.activated = json.activated;
- saveSettings(settings);
- }
- });
- }
- }).catch(function(error){
- console.log(error);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement