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

Commit

Permalink
Fixed some usability issues.
Browse files Browse the repository at this point in the history
Fixed a bug that prevented pushes from being sent when the device was available to purchase.
  • Loading branch information
Daniel Cormier committed Nov 28, 2014
1 parent fdcc638 commit 560893c
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 44 deletions.
13 changes: 8 additions & 5 deletions iwantnexus.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ chrome.storage.sync.get({
refresh_interval:1000
}, function(items) {
setTimeout(function() {
if(window.location.pathname == "/store/cart") {
if(window.location.pathname == "/store/cart") {
console.log('The device is available.');
jQuery('#hardware-checkout').click();
document.getElementById("result").insertAdjacentHTML("beforeEnd", JSON.stringify(res, null, 4) + "\n");
//document.getElementById("result").insertAdjacentHTML("beforeEnd", JSON.stringify(res, null, 4) + "\n");
chrome.storage.sync.get({
api_key: '',
devices:[]
}, function(items) {
PushBullet.APIKey = items.api_key;
console.log('Pushing to ' + items.devices.length + ' devices');

$.each(items.devices, function(index, device_iden){
PushBullet.push("link", device_iden, null, {title: "I got you a Nexus 6", url: currenturl});
console.log('Pushing to device ' + index +': ' + device_iden);
PushBullet.push("link", device_iden, null, {title: "Device available!", url: currenturl});
});
});
} else {
Expand All @@ -24,8 +28,7 @@ chrome.storage.sync.get({
}, items.refresh_interval);
});


function send_pushes()
{

}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "I want nexus",
"description": "I want nexus",
"version": "0.0.2",
"version": "0.0.3",
"content_scripts": [
{
"matches": ["https://play.google.com/store/devices/*"],
Expand Down
144 changes: 106 additions & 38 deletions options.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,126 @@
var devices = [];

$("#save").click(function(e) {
function checked_devices() {
devices = [];
$(".devices").filter(':checked').each(function(index, element) {
devices.push($(element).val());
});

chrome.storage.sync.set({
refresh_interval: $("#refresh_interval").val(),
api_key: $("#api_key").val(),
devices: devices
});

PushBullet.APIKey = $("#api_key").val();
load_devices(devices);
});
console.log(devices.length + ' devices checked');

return devices;
}

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;
devices = items.devices;
load_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()
{
function load_devices(saved_devices, removeInvalidIds)
{
$("#devices").empty().append(
$("<div>").append("DO NOT SELECT CHROME DEVICES OR YOU MAY END UP IN A NEVERENDING LOOP")
$("<div>").append("<small>To avoid a neverending loop, Chrome devices are not included.</small>")
);

PushBullet.devices(function(err, res) {
if(err) {
throw err;
} else {
$.each(res.devices, function(index, device)
{
$("#devices").append(
$("<div>").append(
$("<input>").attr("type", "checkbox").addClass("devices").attr("id",device.iden).val(device.iden).prop('checked', devices.indexOf(device.iden) !== -1)
).append(
$("<label>").attr("for",device.iden).append(device.nickname)
)
);
});
}
else {
active_devices = res.devices.filter(filterOutInactiveDevices);
non_chrome_devices = active_devices.filter(filterOutChromeDevices);

if (non_chrome_devices.length > 0) {
non_chrome_devices.sort(sortByDeviceNickname);

$.each(non_chrome_devices, function(index, device)
{
if (device.kind === 'chrome') {
return;
}

$("#devices").append(
$("<div>").append(
$("<input>").attr("type", "checkbox").addClass("devices").attr("id",device.iden).val(device.iden).prop('checked', saved_devices.indexOf(device.iden) !== -1)
).append(
$("<label>").attr("for",device.iden).append(device.nickname + ' (' + device.type + ')')
)
);
});
}
else {
if (active_devices.length > 0) {
$("#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>"));
}
else {
$("#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>"));
}
}

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 filterOutChromeDevices(device) {
var isChrome = device.kind === 'chrome';

if (isChrome) {
console.log('Skipping Chrome device "' + device.nickname + '" (' + device.iden + ')');
}

return !isChrome;
}

function filterOutInactiveDevices(device) {
return device.active;
}

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){
$.each(devices, function(index, device_iden){
$.each(checked_devices(), function(index, device_iden) {
PushBullet.push("link", device_iden, null, {title: "I got you a Nexus 6", url: "https://www.google.com/"});
});
});
});

0 comments on commit 560893c

Please sign in to comment.