Skip to content
This repository was archived by the owner on Mar 29, 2018. It is now read-only.

Commit 560893c

Browse files
author
Daniel Cormier
committed
Fixed some usability issues.
Fixed a bug that prevented pushes from being sent when the device was available to purchase.
1 parent fdcc638 commit 560893c

File tree

3 files changed

+115
-44
lines changed

3 files changed

+115
-44
lines changed

iwantnexus.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ chrome.storage.sync.get({
66
refresh_interval:1000
77
}, function(items) {
88
setTimeout(function() {
9-
if(window.location.pathname == "/store/cart") {
9+
if(window.location.pathname == "/store/cart") {
10+
console.log('The device is available.');
1011
jQuery('#hardware-checkout').click();
11-
document.getElementById("result").insertAdjacentHTML("beforeEnd", JSON.stringify(res, null, 4) + "\n");
12+
//document.getElementById("result").insertAdjacentHTML("beforeEnd", JSON.stringify(res, null, 4) + "\n");
1213
chrome.storage.sync.get({
1314
api_key: '',
1415
devices:[]
1516
}, function(items) {
1617
PushBullet.APIKey = items.api_key;
18+
console.log('Pushing to ' + items.devices.length + ' devices');
19+
1720
$.each(items.devices, function(index, device_iden){
18-
PushBullet.push("link", device_iden, null, {title: "I got you a Nexus 6", url: currenturl});
21+
console.log('Pushing to device ' + index +': ' + device_iden);
22+
PushBullet.push("link", device_iden, null, {title: "Device available!", url: currenturl});
1923
});
2024
});
2125
} else {
@@ -24,8 +28,7 @@ chrome.storage.sync.get({
2428
}, items.refresh_interval);
2529
});
2630

27-
2831
function send_pushes()
2932
{
3033

31-
}
34+
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "I want nexus",
33
"description": "I want nexus",
4-
"version": "0.0.2",
4+
"version": "0.0.3",
55
"content_scripts": [
66
{
77
"matches": ["https://play.google.com/store/devices/*"],

options.js

Lines changed: 106 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,126 @@
1-
var devices = [];
2-
3-
$("#save").click(function(e) {
1+
function checked_devices() {
42
devices = [];
53
$(".devices").filter(':checked').each(function(index, element) {
64
devices.push($(element).val());
75
});
86

9-
chrome.storage.sync.set({
10-
refresh_interval: $("#refresh_interval").val(),
11-
api_key: $("#api_key").val(),
12-
devices: devices
13-
});
14-
15-
PushBullet.APIKey = $("#api_key").val();
16-
load_devices(devices);
17-
});
7+
console.log(devices.length + ' devices checked');
8+
9+
return devices;
10+
}
1811

19-
chrome.storage.sync.get({
20-
api_key: '',
21-
devices:[],
22-
refresh_interval:1000
23-
}, function(items) {
24-
$("#api_key").val(items.api_key);
25-
$("#refresh_interval").val(items.refresh_interval);
26-
PushBullet.APIKey = items.api_key;
27-
devices = items.devices;
28-
load_devices();
12+
$(function() {
13+
chrome.storage.sync.get({
14+
api_key: '',
15+
devices:[],
16+
refresh_interval:1000
17+
}, function(items) {
18+
$("#api_key").val(items.api_key);
19+
$("#refresh_interval").val(items.refresh_interval);
20+
PushBullet.APIKey = items.api_key;
21+
22+
console.log('Loaded ' + items.devices.length + ' saved devices.');
23+
24+
load_devices(items.devices, true);
25+
})
2926
});
3027

31-
function load_devices()
32-
{
28+
function load_devices(saved_devices, removeInvalidIds)
29+
{
3330
$("#devices").empty().append(
34-
$("<div>").append("DO NOT SELECT CHROME DEVICES OR YOU MAY END UP IN A NEVERENDING LOOP")
31+
$("<div>").append("<small>To avoid a neverending loop, Chrome devices are not included.</small>")
3532
);
33+
3634
PushBullet.devices(function(err, res) {
3735
if(err) {
3836
throw err;
39-
} else {
40-
$.each(res.devices, function(index, device)
41-
{
42-
$("#devices").append(
43-
$("<div>").append(
44-
$("<input>").attr("type", "checkbox").addClass("devices").attr("id",device.iden).val(device.iden).prop('checked', devices.indexOf(device.iden) !== -1)
45-
).append(
46-
$("<label>").attr("for",device.iden).append(device.nickname)
47-
)
48-
);
49-
});
37+
}
38+
else {
39+
active_devices = res.devices.filter(filterOutInactiveDevices);
40+
non_chrome_devices = active_devices.filter(filterOutChromeDevices);
41+
42+
if (non_chrome_devices.length > 0) {
43+
non_chrome_devices.sort(sortByDeviceNickname);
44+
45+
$.each(non_chrome_devices, function(index, device)
46+
{
47+
if (device.kind === 'chrome') {
48+
return;
49+
}
50+
51+
$("#devices").append(
52+
$("<div>").append(
53+
$("<input>").attr("type", "checkbox").addClass("devices").attr("id",device.iden).val(device.iden).prop('checked', saved_devices.indexOf(device.iden) !== -1)
54+
).append(
55+
$("<label>").attr("for",device.iden).append(device.nickname + ' (' + device.type + ')')
56+
)
57+
);
58+
});
59+
}
60+
else {
61+
if (active_devices.length > 0) {
62+
$("#devices").append($("<div>").append("<em>Only Chrome devices are registered with PushBullet! Pushing to those will result in an endless loop. You should <a href=\"https://www.pushbullet.com/apps\" target=\"_blank\">add PushBullet to your other devices</a>.</em>"));
63+
}
64+
else {
65+
$("#devices").append($("<div>").append("<em>No devices registered with PushBullet! You should <a href=\"https://www.pushbullet.com/apps\" target=\"_blank\">fix that</a>.</em>"));
66+
}
67+
}
68+
69+
if (removeInvalidIds && saved_devices && saved_devices.length > 0) {
70+
console.log('Saving loaded, selected devices to remove any invalid device IDs');
71+
72+
// Re-save the devices after loading to clean up any invalid device IDs that we had.
73+
chrome.storage.sync.set({
74+
devices: checked_devices()
75+
});
76+
}
5077
}
5178
});
5279
}
5380

81+
function filterOutChromeDevices(device) {
82+
var isChrome = device.kind === 'chrome';
83+
84+
if (isChrome) {
85+
console.log('Skipping Chrome device "' + device.nickname + '" (' + device.iden + ')');
86+
}
87+
88+
return !isChrome;
89+
}
90+
91+
function filterOutInactiveDevices(device) {
92+
return device.active;
93+
}
94+
95+
function sortByDeviceNickname(a, b) {
96+
if (a.nickname > b.nickname) {
97+
return 1;
98+
}
99+
100+
if (a.nickname < b.nickname) {
101+
return -1;
102+
}
103+
104+
return 0;
105+
}
106+
107+
$("#save").click(function(e) {
108+
console.log('Getting devices to save.');
109+
devices = checked_devices();
110+
api_key = $("#api_key").val();
111+
112+
chrome.storage.sync.set({
113+
refresh_interval: $("#refresh_interval").val(),
114+
api_key: api_key,
115+
devices: devices
116+
});
117+
118+
PushBullet.APIKey = api_key;
119+
load_devices(devices, false);
120+
});
121+
54122
$("#test").click(function(e){
55-
$.each(devices, function(index, device_iden){
123+
$.each(checked_devices(), function(index, device_iden) {
56124
PushBullet.push("link", device_iden, null, {title: "I got you a Nexus 6", url: "https://www.google.com/"});
57125
});
58-
});
126+
});

0 commit comments

Comments
 (0)