Skip to content
Permalink
Browse files

Rewiring to use chrome.storage.local

  • Loading branch information
jeffjose committed Oct 20, 2017
1 parent 5f453f9 commit 7d06d66a9a65bc261b722a07b5481ed03ac22ca0
BIN +2.91 KB logo.png
Binary file not shown.
@@ -0,0 +1,70 @@
/**
* @fileoverview Description of this file.
*/

var globals = {};
globals.tabToUrl = {};

function handleTab(tab){

chrome.tabs.sendMessage(tab.id, {
action: 'handleTab',
tabId: tab.id
})

};

fetch('vendors.json').then(function (response) {

response.json().then(function (data){

if (response.ok) {

alert('setting vendors.json')
vendors = {'vendors': data.vendors}
chrome.storage.local.set(vendors)
}

else {
Promise.reject({status: response.status})
}

})

})
.catch(error => console.error('vendors.json in the readiness tool is invalid', error))

/**
* Listen for a new tab being created.
*/
chrome.tabs.onCreated.addListener(function(tab) {
handleTab(tab);


});

/**
* Listen for a tab being changed.
*/
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
globals.tabToUrl[tabId] = tab.url;
handleTab(tab);
});

/**
* Listen for a tab being removed.
*/
chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
window.sessionStorage.removeItem(globals.tabToUrl[tabId]);
});

/**
* Listen for a tab being replaced (due to prerendering or instant).
*/
chrome.tabs.onReplaced.addListener(function(addedTabId, removedTabId) {
window.sessionStorage.removeItem(globals.tabToUrl[removedTabId]);
alert('onReplaced: ' + addedTabId)
chrome.tabs.get(addedTabId, function(tab) {
handleTab(tab);
});
});
@@ -0,0 +1,212 @@
/**
* @fileoverview Description of this file.
*/

/**
* Callback function that sends a response upon receiving message
* @param {!Object} request - Message Object
* @param {!Object} sender - Message sender defined here
*/
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action == 'handleTab') {
console.log(document.location)
findDetectedVendors(document.documentElement.innerHTML, request.tabId)

}

});

/**
* Returns all the 3rd party applications found on the website
* @param {string} html - String containing all HTML on the page
* @return {Object}
*/
function findDetectedVendors(html, tabId) {

vendors = chrome.storage.local.get('vendors', function(vendorsData){
vendors = vendorsData.vendors

detectedVendors = filteredVendors(html, vendors);
//alert('detectedVendors .. ' + JSON.stringify(detectedVendors))

console.log('detetctedVendors', detectedVendors)

data = {}
data[tabId] = {'detectedVendors': detectedVendors}
chrome.storage.local.set(data)

})

//fetch('vendors.json').then(function (response) {
// response.json().then(function (data) {
// if (response.ok) {
// let listAllVendors = data.vendors;
// detectedVendors = filteredVendors(html, listAllVendors);

// data = {}
// data[tabId] = {'detectedVendors': detectedVendors, 'listAllVendors': listAllVendors}
// chrome.storage.local.set(data)

// //alert('showing ..' + JSON.stringify(detectedVendors))
// //console.log('showing')
// //showSupportedVendorsInView(detectedVendors, listAllVendors);

// return detectedVendors;
// } else {
// return Promise.reject({status: response.status});
// }
// });
//})
//.catch(error => console.error('vendors.json in the readiness tool is invalid.',
//error));
}

/**
* Splits all detected vendors into 'supported' and 'not supported'
* @param {string} htmlString - String containing all HTML on the page
* @param {!Object} listAllVendors - JSON of all the 3p vendors
* @return {Object}
*/
function filteredVendors(htmlString, listAllVendors) {
/** @type {FilteredVendorsDef} */
const filteredVendors = {
'supported': {
'ads': [],
'analytics': []
},
'notSupported': {
'ads': [],
'analytics': []
}
};
// for all the vendor objects in the vendors.json file
Object.keys(listAllVendors).forEach(function (vendorName) {

const vendorConfig = listAllVendors[vendorName];
// If object has a 'regex' key
if (vendorConfig.regex) {
vendorConfig.regex.forEach(function (x) {
if (vendorConfig.category.length == 0) {
console.error('The vendor', vendorName,
'does not have a value for "category" in vendors.json');
return;
} else if (vendorConfig.category != "Ads" && vendorConfig.category != "Analytics") {
console.error('The vendor',
vendorName,
'is not declared as an ads or analytics vendor in vendors.json');
return;
}
addToDict(x, htmlString, filteredVendors, vendorName,
vendorConfig.category);
});
}
});
return filteredVendors;
}

/**
* Pushes vendor names to the supported or not supported list of the object
* 'filteredVendors'
* @param {string} regexString - String representation of regular expression
* @param {!string} htmlString - String containing all HTML on the page
* @param {FilteredVendorsDef} filteredVendors - Object separating the 3P services by
* support
* @param {string} vendorName - name of third party service
* @param {string} category - the category that the key belongs to
*/
function addToDict(regexString, htmlString, filteredVendors, vendorName, category) {

console.log('Checking')

const regX = new RegExp(regexString);

if (regX.test(htmlString)) {
if (isVendorNameUnique(filteredVendors, vendorName)) {
switch (category) {
case 'Analytics':
if (isSupported(vendorName)) {



filteredVendors.supported.analytics.push(vendorName);
} else {
filteredVendors.notSupported.analytics.push(vendorName);
}
break;
case 'Ads':
if (isSupported(vendorName)) {
filteredVendors.supported.ads.push(vendorName);
} else {
filteredVendors.notSupported.ads.push(vendorName);
}
break;
}
}
}
}


/**
* Checks to see if vendorName is unique within the object
* @param {Object} obj - Object separating the 3p services by support
* @param {string} vendorName - name of third party service
* @return {boolean}
*/
function isVendorNameUnique(obj, vendorName) {
let count = 0;
if (obj.supported.ads.includes(vendorName)) {
count ++;
}
if (obj.supported.analytics.includes(vendorName)) {
count ++;
}
if (obj.notSupported.ads.includes(vendorName)) {
count ++;
}
if (obj.notSupported.analytics.includes(vendorName)) {
count ++;
}
return count < 1;
}

/**
* TODO (alwalton@): get list of supported ads/analytics programatically
* Check if vendor is in supported list of vendor names
* @param {string} vendorName - name of vendor
* @return {boolean}
*/
function isSupported(vendorName) {
const ampSupported = [
'A8', 'A9', 'AcccessTrade', 'Adblade', 'Adform', 'Adfox', 'Ad Generation',
'Adhese', 'ADITION', 'Adman', 'AdmanMedia', 'AdReactor', 'AdSense',
'AdsNative', 'AdSpirit', 'AdSpeed', 'AdStir', 'AdTech', 'AdThrive',
'Ad Up Technology', 'Adverline', 'Adverticum', 'AdvertServe',
'Affiliate-B', 'AMoAd', 'AppNexus', 'Atomx', 'Bidtellect',
'brainy', 'CA A.J.A. Infeed', 'CA-ProFit-X', 'Chargeads', 'Colombia',
'Content.ad', 'Criteo', 'CSA', 'CxenseDisplay', 'Dianomi', 'DistroScale',
'Dot and Media', 'Doubleclick', 'DoubleClick for Publishers (DFP)',
'DoubleClick Ad Exchange (AdX)', 'E-Planning', 'Ezoic', 'FlexOneELEPHANT',
'FlexOneHARRIER', 'fluct', 'Felmat', 'Flite', 'Fusion', 'Google AdSense',
'GenieeSSP', 'GMOSSP', 'GumGum', 'Holder', 'Imedia', 'I-Mobile',
'iBillboard', 'Improve Digital', 'Index Exchange', 'Industrybrains',
'InMobi', 'Kargo', 'Kiosked', 'Kixer', 'Ligatus', 'LOKA', 'MADS',
'MANTIS', 'MediaImpact', 'Media.net', 'Mediavine', 'Meg', 'MicroAd',
'Mixpo', 'myWidget', 'Nativo', 'Navegg', 'Nend', 'NETLETIX', 'Nokta',
'Open AdStream (OAS)', 'OpenX', 'plista', 'polymorphicAds', 'popin',
'PubMatic', 'Pubmine', 'PulsePoint', 'Purch', 'Rambler&Co', 'Relap',
'Revcontent', 'Rubicon Project', 'Sharethrough', 'Sklik', 'SlimCut Media',
'Smart AdServer', 'smartclip', 'Sortable', 'SOVRN', 'SpotX', 'SunMedia',
'Swoop', 'Teads', 'TripleLift', 'ValueCommerce', 'Webediads', 'Weborama',
'Widespace', 'Xlift', 'Yahoo', 'YahooJP', 'Yandex', 'Yieldbot', 'Yieldmo',
'Yieldone', 'Zedo', 'Zucks', 'Bringhub', 'Outbrain', 'Taboola', 'ZergNet',
'Acquia Lift', 'Adobe Analytics', 'AFS Analytics', 'AT Internet', 'Burt',
'Baidu Analytics', 'Chartbeat', 'Clicky Web Analytics', 'comScore',
'Cxense', 'Dynatrace', 'Eulerian Analytics', 'Gemius', 'Google AdWords',
'Google Analytics', 'INFOnline / IVW', 'Krux', 'Linkpulse', 'Lotame',
'Médiamétrie', 'mParticle', 'Nielsen', 'OEWA', 'Parsely', 'Piano',
'Quantcast Measurement', 'Segment', 'SOASTA mPulse', 'SimpleReach',
'Snowplow Analytics', 'Webtrekk', 'Yandex Metrica'
, ];
// If it is NOT in list of supported vendors
return ampSupported.includes(vendorName);
}

This file was deleted.

@@ -1,9 +1,9 @@
{
"manifest_version": 2,

"name": "AMP Readiness Tool",
"name": "xAMP Readiness Tool",
"description": "See at a glance the various third party vendors on the page and whether they have AMP support.",
"version": "1.0",
"version": "1.3",

"icons": {
"16": "amp.png",
@@ -15,8 +15,15 @@
"default_popup": "popup.html"
},
"background": {
"scripts": ["getPagesSource.js"]
"scripts": ["background.js"]
},
"content_scripts": [
{
"js": ["content_script.js"],
"matches": ["<all_urls>"],
"all_frames": false
}
],
"web_accessible_resources": [
"apps.json"
],
@@ -25,6 +32,8 @@
"<all_urls>",
"webNavigation",
"tabs",
"https://ajax.googleapis.com/"
"https://ajax.googleapis.com/",
"storage",
"unlimitedStorage"
]
}

0 comments on commit 7d06d66

Please sign in to comment.
You can’t perform that action at this time.