Created
January 8, 2015 01:58
-
-
Save dvliman/f7ed98edb8b9819277a9 to your computer and use it in GitHub Desktop.
coupon-at-checkout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Coupons at Checkout Browser Extension | |
* Copyright (c) 2012, 2013 CouponFollow, LLC. All rights reserved. | |
* Copying this source code in any manner is strictly prohibited. | |
*/ | |
"use strict"; | |
var storage = chrome.storage.local; | |
var cf = { | |
history: [], | |
helpers: {}, | |
blacklist: {}, | |
updates: {}, | |
init: function() { | |
this.checkVersion(); | |
storage.get({helpers: {}, blacklist: {}, updates: {}}, function(items) { | |
cf.helpers = items.helpers; | |
cf.blacklist = items.blacklist; | |
cf.updates = items.updates; | |
cf.checkUpdates(); | |
}); | |
chrome.extension.onRequest.addListener(requestHandler); | |
}, | |
checkVersion: function() { | |
var currVersion = chrome.app.getDetails().version; | |
var prevVersion = localStorage['version'] | |
if (currVersion != prevVersion) { | |
if (typeof prevVersion == 'undefined') { | |
chrome.tabs.create({url: 'http://couponfollow.com/checkout/chrome/success'}); | |
} else { | |
//chrome.tabs.create({url: 'http://couponfollow.com/checkout/chrome/updated'}); | |
} | |
localStorage['version'] = currVersion; | |
} | |
}, | |
checkUpdates: function() { | |
remoteGet('http://couponfollow.com/files/addon/catc_updates.json', function(updates) { | |
// Check and update helpers | |
var helpersLocalTime = Date.parse(cf.updates.helpersUpdated); | |
var helpersRemoteTime = Date.parse(updates.helpersUpdated); | |
if (helpersLocalTime) { | |
if (helpersLocalTime < helpersRemoteTime) { | |
cf.updateHelpers(); | |
} | |
} else { | |
cf.updateHelpers(); | |
} | |
// Check and update blacklist | |
var blacklistLocalTime = Date.parse(cf.updates.blacklistUpdated); | |
var blacklistRemoteTime = Date.parse(updates.blacklistUpdated); | |
if (blacklistLocalTime) { | |
if (blacklistLocalTime < blacklistRemoteTime) { | |
cf.updateBlacklist(); | |
} | |
} else { | |
cf.updateBlacklist(); | |
} | |
cf.updates = updates; | |
storage.set({'updates': cf.updates}); | |
}); | |
}, | |
updateHelpers: function() { | |
remoteGet('http://couponfollow.com/files/addon/catc_helpers.json', function(data) { | |
cf.helpers = {}; | |
if (data.records > 0) { | |
for (var i = 0; i < data.helpers.length; i++) { | |
var helper = data.helpers[i]; | |
cf.helpers[helper.domainName] = helper; | |
} | |
} | |
storage.set({'helpers': cf.helpers}); | |
}); | |
}, | |
updateBlacklist: function() { | |
remoteGet('http://couponfollow.com/files/addon/catc_blacklist.json', function(data) { | |
cf.blacklist = {}; | |
for (var i = 0; i < data.blacklistDomains.length; i++) { | |
var domain = data.blacklistDomains[i]; | |
cf.blacklist[domain] = "1"; | |
} | |
storage.set({'blacklist': cf.blacklist}); | |
}); | |
} | |
}; | |
setTimeout(function () { cf.init(); }, 1000); | |
function requestHandler(request, sender, sendResponse) { | |
log('requestHandler: call = ' + request.call); | |
if (request.call == "pasteCoupon") { | |
pasteCoupon(request.arg); | |
} else if (request.call == "cf") { | |
sendResponse(cf); | |
} else if (request.call == "markVisited") { | |
markVisited(request.arg); | |
} else if (request.call == "getHistory") { | |
getHistory(request.arg, sendResponse); | |
} else if (request.call == "getHighlight") { | |
getHighlight(sendResponse); | |
} else { | |
sendResponse({}); | |
} | |
} | |
function pasteCoupon(data) { | |
if (data.nt == 1) { | |
chrome.tabs.create({ | |
url: 'http://couponfollow.com/code/go/' + data.id, | |
active: false | |
}); | |
} | |
chrome.tabs.getSelected(null, function (tab) { | |
chrome.tabs.sendMessage(tab.id, data); | |
}); | |
} | |
function markVisited(domain) { | |
chrome.tabs.getSelected(null, function (tab) { | |
if (cf.history[tab.id] == undefined) { | |
cf.history[tab.id] = []; | |
} | |
if (!contains(cf.history[tab.id], domain)) { | |
if (cf.history[tab.id].length == 3) { | |
cf.history[tab.id].shift(); | |
} | |
cf.history[tab.id].push(domain); | |
} | |
}); | |
} | |
function getHistory(data, sendResponse) { | |
chrome.tabs.getSelected(null, function (tab) { | |
cf.history[tab.id].pop(); | |
var toSend = { | |
history: cf.history[tab.id].join(','), | |
point: data.point | |
}; | |
sendResponse(toSend); | |
}); | |
} | |
function getHighlight(sendResponse) { | |
if (localStorage.highlight == undefined) { | |
localStorage.highlight = "true"; | |
} | |
sendResponse({highlight: localStorage.highlight}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment