-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
64 lines (54 loc) · 1.87 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
'use strict';
var API_KEY = 'FILL_THIS_IN', // Do not push the API key to the repo!
FORM_URL_REGEX = '^https://docs\.google\.com/forms/.+/viewform([?].*)?$';
// Set the rule when the extension is installed or upgraded.
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([
{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { urlMatches: FORM_URL_REGEX },
})
],
actions: [
new chrome.declarativeContent.ShowPageAction()
]
}
]);
});
});
// Send an toggle request when the page action is clicked.
chrome.pageAction.onClicked.addListener(function (tab) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { type: "toggle"});
});
});
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
switch (request.type) {
case 'api':
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://ritstar.com/api/member?id=' + request.memberId + '&key=' + API_KEY, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
sendResponse({
aoeu: 500,
status: xhr.status,
url: xhr.responseURL,
responseText: xhr.responseText
});
}
}
xhr.send();
return true; // Indicate a response will be sent asynchronously.
break;
case 'showEnabled':
chrome.pageAction.setTitle({ tabId: sender.tab.id, title: 'STAR ID card scanner enabled' });
chrome.pageAction.setIcon({ tabId: sender.tab.id, path: 'icon.png' });
break;
case 'showDisabled':
chrome.pageAction.setTitle({ tabId: sender.tab.id, title: 'STAR ID card scanner disabled' });
chrome.pageAction.setIcon({ tabId: sender.tab.id, path: 'icon_struck.png' });
break;
}
});