From d4a4bfd3ee38458c09937f00541af3cefe1e695f Mon Sep 17 00:00:00 2001 From: Elbert Alias Date: Mon, 17 Apr 2017 11:13:33 +1000 Subject: [PATCH] Use promises for options in WebExtension driver --- src/drivers/bookmarklet/driver.js | 5 +- src/drivers/phantomjs/driver.js | 17 ++- src/drivers/webextension/css/options.css | 2 +- src/drivers/webextension/js/content.js | 2 + src/drivers/webextension/js/defaults.js | 6 -- src/drivers/webextension/js/driver.js | 129 +++++++++++++++-------- src/drivers/webextension/js/options.js | 74 +++++++------ src/drivers/webextension/manifest.json | 1 + src/drivers/webextension/options.html | 4 - 9 files changed, 145 insertions(+), 95 deletions(-) delete mode 100644 src/drivers/webextension/js/defaults.js diff --git a/src/drivers/bookmarklet/driver.js b/src/drivers/bookmarklet/driver.js index 01cdb4abb..230ff6aaf 100644 --- a/src/drivers/bookmarklet/driver.js +++ b/src/drivers/bookmarklet/driver.js @@ -1,7 +1,10 @@ /** - * Bookmarklet driver + * WebExtension driver */ +/** global: wappalyzer */ +/** global: XMLHttpRequest */ + (function() { if ( typeof wappalyzer === 'undefined' ) { return; diff --git a/src/drivers/phantomjs/driver.js b/src/drivers/phantomjs/driver.js index e7fc9b621..cf4d97901 100644 --- a/src/drivers/phantomjs/driver.js +++ b/src/drivers/phantomjs/driver.js @@ -1,3 +1,10 @@ +/** + * PhantomJS driver + */ + +/** global: phantom */ +/** global: wappalyzer */ + (function() { var url, @@ -16,7 +23,9 @@ require('fs').changeWorkingDirectory(scriptDir); require('system').args.forEach(function(arg, i) { - var arr = /^(--[^=]+)=(.+)$/.exec(arg); + var + value, + arr = /^(--[^=]+)=(.+)$/.exec(arg); if ( arr && arr.length === 3 ) { arg = arr[1]; @@ -35,7 +44,9 @@ break; case '--resource-timeout': - resourceTimeout = value; + if ( value ) { + resourceTimeout = value; + } break; default: @@ -112,7 +123,7 @@ init: function() { var page, hostname, - headers = {}; + headers = {}, a = document.createElement('a'), json = JSON.parse(require('fs').read('apps.json')); diff --git a/src/drivers/webextension/css/options.css b/src/drivers/webextension/css/options.css index a5398318c..090242223 100644 --- a/src/drivers/webextension/css/options.css +++ b/src/drivers/webextension/css/options.css @@ -83,7 +83,7 @@ a:active { #about { border-top: 1px solid #dbdbdb; - margin-top: 2.5rem; + margin-top: 1.5rem; padding: 1.5rem 0 0 0; } diff --git a/src/drivers/webextension/js/content.js b/src/drivers/webextension/js/content.js index e394e8eb7..ac21ee724 100644 --- a/src/drivers/webextension/js/content.js +++ b/src/drivers/webextension/js/content.js @@ -1,3 +1,5 @@ +/** global: browser */ + (function() { var c = { init: function() { diff --git a/src/drivers/webextension/js/defaults.js b/src/drivers/webextension/js/defaults.js deleted file mode 100644 index 98ce709d0..000000000 --- a/src/drivers/webextension/js/defaults.js +++ /dev/null @@ -1,6 +0,0 @@ -var defaults = { - autoAnalyzeHeaders: 0, - upgradeMessage: 1, - dynamicIcon: 1, - tracking: 1 -}; diff --git a/src/drivers/webextension/js/driver.js b/src/drivers/webextension/js/driver.js index 164ce1c0b..cdefcefe6 100644 --- a/src/drivers/webextension/js/driver.js +++ b/src/drivers/webextension/js/driver.js @@ -24,6 +24,26 @@ console.log('[wappalyzer ' + args.type + '] ' + args.message); }, + /** + * Get a value from localStorage + */ + getOption: function(name, defaultValue, callback) { + browser.storage.local.get(name).then(function(item) { + callback(item.hasOwnProperty(name) ? item[name] : defaultValue); + }); + }, + + /** + * Set a value in localStorage + */ + setOption: function(name, value) { + var option = {}; + + option[name] = value; + + browser.storage.local.set(option); + }, + /** * Initialize */ @@ -54,19 +74,27 @@ try { var version = browser.runtime.getManifest().version; - if ( localStorage['version'] == null ) { - firstRun = true; - - // Set defaults - for ( var option in defaults ) { - localStorage[option] = defaults[option]; + w.driver.getOption('version', null, function(previousVersion) { + if ( previousVersion === null ) { + w.driver.goToURL({ + url: w.config.websiteURL + 'installed' + }); + } else if ( version !== previousVersion ) { + w.driver.getOption('upgradeMessage', true, function(upgradeMessage) { + if ( upgradeMessage ) { + w.driver.goToURL({ + url: w.config.websiteURL + 'upgraded', + background: true + }); + } + }); } - } else if ( version !== localStorage['version'] && parseInt(localStorage['upgradeMessage'], 10) ) { - upgraded = true; - } - localStorage['version'] = version; - } catch(e) { } + w.driver.setOption('version', version); + }); + } catch(e) { + // Do nothing + } if ( typeof chrome === 'undefined' ) { browser.runtime.onMessage.addListener(w.driver.onMessage); @@ -183,10 +211,14 @@ }, + /** + * Open a tab + */ goToURL: function(args) { - var url = args.url + ( typeof args.medium === 'undefined' ? '' : '?pk_campaign=chrome&pk_kwd=' + args.medium); - - browser.tabs.create({ url: url, active: args.background === undefined || !args.background }); + browser.tabs.create({ + url: args.url, + active: args.background === undefined || !args.background + }); }, /** @@ -208,37 +240,42 @@ tabCache[tab.id].appsDetected = w.detected[url]; if ( count > 0 ) { - // Find the main application to display - var appName, found = false; + w.driver.getOption('dynamicIcon', true, function(dynamicIcon) { + var appName, found = false; - w.driver.categoryOrder.forEach(function(match) { - for ( appName in w.detected[url] ) { - w.apps[appName].cats.forEach(function(cat) { - var icon = w.apps[appName].icon || 'default.svg'; + // Find the main application to display + w.driver.categoryOrder.forEach(function(match) { + for ( appName in w.detected[url] ) { + w.apps[appName].cats.forEach(function(cat) { + var icon = w.apps[appName].icon || 'default.svg'; - if ( !parseInt(localStorage['dynamicIcon'], 10) ) { - icon = 'default.svg'; - } - - if ( cat == match && !found ) { - if ( /\.svg$/i.test(icon) ) { - icon = 'converted/' + icon + '.png'; + if ( !dynamicIcon ) { + icon = 'default.svg'; } - browser.pageAction.setIcon({ tabId: tab.id, path: 'images/icons/' + icon }); + if ( cat === match && !found ) { + if ( /\.svg$/i.test(icon) ) { + icon = 'converted/' + icon + '.png'; + } - found = true; - } - }); + browser.pageAction.setIcon({ + tabId: tab.id, + path: 'images/icons/' + icon + }); + + found = true; + } + }); + } + }); + + if ( typeof chrome !== 'undefined' ) { + // Browser polyfill doesn't seem to work here + chrome.pageAction.show(tab.id); + } else { + browser.pageAction.show(tab.id); } }); - - if ( typeof chrome !== 'undefined' ) { - // Browser polyfill doesn't seem to work here - chrome.pageAction.show(tab.id); - } else { - browser.pageAction.show(tab.id); - } }; }, @@ -246,17 +283,19 @@ * Anonymously track detected applications for research purposes */ ping: function() { - if ( Object.keys(w.ping.hostnames).length && parseInt(localStorage['tracking'], 10) ) { - w.driver.post('http://ping.wappalyzer.com/v2/', w.ping); + w.driver.getOption('tracking', true, function(tracking) { + if ( Object.keys(w.ping.hostnames).length && tracking ) { + w.driver.post('http://ping.wappalyzer.com/v2/', w.ping); - w.log('w.driver.ping: ' + JSON.stringify(w.ping)); + w.log('w.driver.ping: ' + JSON.stringify(w.ping)); - w.ping = { hostnames: {} }; + w.ping = { hostnames: {} }; - w.driver.post('https://ad.wappalyzer.com/log/wp/', w.adCache); + w.driver.post('https://ad.wappalyzer.com/log/wp/', w.adCache); - w.adCache = []; - } + w.adCache = []; + } + }); }, /** diff --git a/src/drivers/webextension/js/options.js b/src/drivers/webextension/js/options.js index 3a3aeac2d..d73b869ed 100644 --- a/src/drivers/webextension/js/options.js +++ b/src/drivers/webextension/js/options.js @@ -1,57 +1,61 @@ +/** global: localStorage */ + document.addEventListener('DOMContentLoaded', function() { var d = document; var options = { - opts: defaults, - init: function() { options.load(); - d.getElementById('github' ).addEventListener('click', function() { window.open(wappalyzer.config.githubURL); }); - d.getElementById('twitter' ).addEventListener('click', function() { window.open(wappalyzer.config.twitterURL); }); - d.getElementById('wappalyzer').addEventListener('click', function() { window.open(wappalyzer.config.websiteURL + '?pk_campaign=chrome&pk_kwd=options'); }); + d.querySelector('#github' ).addEventListener('click', function() { open(wappalyzer.config.githubURL); }); + d.querySelector('#twitter' ).addEventListener('click', function() { open(wappalyzer.config.twitterURL); }); + d.querySelector('#wappalyzer').addEventListener('click', function() { open(wappalyzer.config.websiteURL); }); + }, - d.getElementById('options-save').addEventListener('click', options.save); + get: function(name, defaultValue, callback) { + browser.storage.local.get(name).then(function(item) { + callback(item.hasOwnProperty(name) ? item[name] : defaultValue); + }); }, - load: function() { - var option, value; + set: function(name, value) { + var option = {}; - for ( option in options.opts ) { - if ( value = localStorage[option] ) { - options.opts[option] = value; - } - } + option[name] = value; - if ( parseInt(options.opts.upgradeMessage) ) { - d.getElementById('option-upgrade-message').setAttribute('checked', 'checked'); - } + browser.storage.local.set(option); + }, - if ( parseInt(options.opts.dynamicIcon) ) { - d.getElementById('option-dynamic-icon').setAttribute('checked', 'checked'); - } + load: function() { + options.get('upgradeMessage', true, function(value) { + var el = d.querySelector('#option-upgrade-message'); - if ( parseInt(options.opts.tracking) ) { - d.getElementById('option-tracking').setAttribute('checked', 'checked'); - } - }, + el.checked = value; + + el.addEventListener('change', function() { + options.set('upgradeMessage', el.checked); + }); + }); + + options.get('dynamicIcon', true, function(value) { + var el = d.querySelector('#option-dynamic-icon'); - save: function() { - var option; + el.checked = value; - options.opts.upgradeMessage = d.getElementById('option-upgrade-message').checked ? 1 : 0; - options.opts.dynamicIcon = d.getElementById('option-dynamic-icon' ).checked ? 1 : 0; - options.opts.tracking = d.getElementById('option-tracking' ).checked ? 1 : 0; + el.addEventListener('change', function() { + options.set('dynamicIcon', el.checked); + }); + }); - for ( option in options.opts ) { - localStorage[option] = options.opts[option]; - } + options.get('tracking', true, function(value) { + var el = d.querySelector('#option-tracking'); - d.getElementById('options-saved').style.display = 'inline'; + el.checked = value; - setTimeout(function(){ - d.getElementById('options-saved').style.display = 'none'; - }, 2000); + el.addEventListener('change', function() { + options.set('tracking', el.checked); + }); + }); } }; diff --git a/src/drivers/webextension/manifest.json b/src/drivers/webextension/manifest.json index 1bc2a6e5c..79d8409e9 100644 --- a/src/drivers/webextension/manifest.json +++ b/src/drivers/webextension/manifest.json @@ -63,6 +63,7 @@ "open_in_tab": false }, "permissions": [ + "storage", "tabs", "webRequest", "webNavigation", diff --git a/src/drivers/webextension/options.html b/src/drivers/webextension/options.html index b3a853619..755b909e2 100644 --- a/src/drivers/webextension/options.html +++ b/src/drivers/webextension/options.html @@ -33,10 +33,6 @@

-

- Saved -

-