This repository has been archived by the owner on Mar 29, 2018. It is now read-only.
forked from wolfd/iwantnexus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
173 lines (136 loc) · 4.14 KB
/
options.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
function checked_devices() {
devices = [];
$('#devices .device > :checkbox').filter(':checked').each(function(index, device) {
devices.push($(device).val());
});
console.log(devices.length + ' devices checked');
return devices;
}
$(function() {
chrome.storage.sync.get(
{
api_key: '',
devices: [],
refresh_interval: 1000
},
function(items) {
$("#api_key").val(items.api_key);
$("#refresh_interval").val(items.refresh_interval);
PushBullet.APIKey = items.api_key;
console.log('Loaded ' + items.devices.length + ' saved devices.');
load_devices(items.devices, true);
}
);
});
function load_devices(saved_devices, removeInvalidIds)
{
var div_devices = $("#devices");
if (!PushBullet.APIKey) {
div_devices.empty().append(
$("<div>").addClass("loading").append($("<small>").append("Enter your Pushbullet Access Token above and click save to see devices to push notifications to."))
);
return;
}
div_devices.empty().append(
$("<div>").addClass("loading").append($("<small>").append("Loading devices from PushBullet..."))
);
PushBullet.devices(function(err, res) {
div_devices.empty();
if(err) {
if (!err.error) {
msg = err.response;
}
else {
msg = err.error.message;
}
div_devices.append(
$("<div>").addClass("error").append($("<small>").append("There was a problem loading devices from PushBullet. This might help: " + msg))
);
throw err.httpStatus + ' ' + msg;
}
else {
active_devices = res.devices.filter(filterOutInactiveDevices);
if (active_devices.length > 0) {
active_devices.sort(sortByDeviceNickname);
var div_non_chrome = $("<div>").addClass("non_chrome");
var div_chrome = $("<div>").addClass("chrome");
div_chrome.append(
$("<div>").append($("<small>").append("To avoid a neverending loop, only push to the following Chrome devices if you know exactly what you're doing."))
);
$.each(active_devices, function(index, device) {
var checeked = saved_devices.indexOf(device.iden) !== -1;
var device_node = buildDeviceNode(device, checeked);
if (isChromeDevice(device)) {
div_chrome.append(device_node);
}
else {
div_non_chrome.append(device_node);
}
});
var non_chrome_count = div_non_chrome.find(".device").length;
var chrome_count = div_chrome.find(".device").length
if (non_chrome_count > 0) {
div_devices.append(div_non_chrome);
if (chrome_count > 0) {
div_devices.append("<br>");
}
}
if (chrome_count > 0) {
div_devices.append(div_chrome);
}
}
if (removeInvalidIds && saved_devices && saved_devices.length > 0) {
console.log('Saving loaded, selected devices to remove any invalid device IDs');
// Re-save the devices after loading to clean up any invalid device IDs that we had.
chrome.storage.sync.set({
devices: checked_devices()
});
}
}
});
}
function buildDeviceNode(device, checked) {
var div_device = $("<div>").addClass("device").append(
$("<input>").attr("type", "checkbox").attr("id",device.iden).val(device.iden).prop('checked', checked)
).append(
$("<label>").attr("for", device.iden).append($('<strong>').append(device.nickname)).append(' ').append($('<small>').append('(' + device.model + ')'))
)
return div_device;
}
function filterOutInactiveDevices(device) {
return device.active;
}
function isChromeDevice(device) {
var isChrome = device && device.kind === 'chrome';
return isChrome;
}
function sortByDeviceNickname(a, b) {
if (a.nickname > b.nickname) {
return 1;
}
if (a.nickname < b.nickname) {
return -1;
}
return 0;
}
$("#save").click(function(e) {
console.log('Getting devices to save.');
devices = checked_devices();
api_key = $("#api_key").val();
chrome.storage.sync.set({
refresh_interval: $("#refresh_interval").val(),
api_key: api_key,
devices: devices
});
PushBullet.APIKey = api_key;
load_devices(devices, false);
});
$("#test").click(function(e) {
var msg = {
title: "Test Push",
url: "https://play.google.com/store/devices"
};
$.each(checked_devices(), function(index, device_iden) {
PushBullet.push("link", device_iden, null, msg);
});
});