-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
130 lines (106 loc) · 4.02 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
Event LISTENERS
background.js --> URL Tracking, Initial Message Receiving(deactivated after first run?? Or how does the background page work)
We Want the message receiving to run once (but not sure whether background page is run when extension is loaded)
popup.js --> Message Passing for change in the User's option (asynchronous)
listener.js --> N/A (although it calls all the event listener from listener2.js)
Note: Button Event Listener is built in the create_button_ui function
listener2.js --> Button Event Listener (not sure if we should fetch from the API once, or remain active)
ChangeParse (basically message passing parser for the OPTIONS)
*/
// Google Analytics Code
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-22982865-1']);
//_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
function logButtonClick(buttonId) {
_gaq.push(['_trackEvent', buttonId, 'clicked']);
}
function logButtonImpression(buttonId) {
_gaq.push(['_trackEvent', buttonId, 'viewed']);
}
// End Google Analytics Code
// Helper Function to log when the listener was created
// Used for background/Popup --> Mainly URL Tracking event listener and Initial Message Listener
function trackListener(listenerName) {
var d = new Date();
var t = d.getTime();
console.log(listenerName + ' was created at time: ' + t);
}
var gmail_tabs;
function listen_gmail() {
chrome.tabs.query({url: '*://mail.google.com/*'}, function(tabs) {
// very serious mistake --> tab_id should be the result's ID, not the object itself
for (i=0; i < tabs.length; ++i) {
gmail_tabs = tabs[0].id;
}
});
}
function listen_chrome() {
chrome.tabs.onUpdated.addListener(function(tabId, info, tab) {
//ensure that tab_id is properly set
listen_gmail();
if (tabId == gmail_tabs) {
chrome.tabs.executeScript(tabId, {file: 'jquery.js'});
chrome.tabs.executeScript(tabId, {file: 'listener2.js'});
chrome.tabs.executeScript(tabId, {file: 'listener.js'});
chrome.tabs.executeScript(tabId, {file: 'send.js'});
}
});
}
function messageListener() {
// Again, we are anticipating the sort of Messages that is being passed
// and hence throw no exceptions/errors, if the user ever attempts to send messages around
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.question) {
sendResponse({answer: localStorage['option']});
}
if (request.buttonClick) {
console.log("Button Click Message from Content Script");
logButtonClick(request.buttonClick);
sendResponse({answer: "Button Click Logged"});
}
if (request.buttonImpression) {
console.log("Button Impression Message from Content Script");
success = logButtonImpression(request.buttonImpression);
if (success) {sendResponse({answer: "Button Impression Failed"});}
else {sendResponse({answer: "Button Impression Logged"});}
}
}
);
}
function onInstall() {
console.log("Extension Installed");
chrome.tabs.create({url: "http://www.lazytruth.com/?page_id=78"});
}
function onUpdate() {
console.log("Extension Updated");
}
function getVersion() {
var details = chrome.app.getDetails();
return details.version;
}
window.onload = function() {
trackListener('URL Change Listener');
messageListener();
trackListener('Initial Message Passing Listener');
localStorage['option'] = 'choice'
listen_chrome();
// Check if the version has changed.
var currVersion = getVersion();
var prevVersion = localStorage['version']
if (currVersion != prevVersion) {
// Check if we just installed this extension.
if (typeof prevVersion == 'undefined') {
onInstall();
} else {
onUpdate();
}
localStorage['version'] = currVersion;
}
};