Last active
January 6, 2017 02:35
-
-
Save harrisonhjones/4568e192ed5723aba02f35c2153e63e9 to your computer and use it in GitHub Desktop.
TamperMonkey Script - PublishEvent for the Particle Console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Publish Event for the Particle Console | |
// @namespace http://harrisonhjones.com | |
// @version 1.5 | |
// @updateURL https://gist.github.com/harrisonhjones/4568e192ed5723aba02f35c2153e63e9/raw/publishEventParticleConsole.user.js | |
// @downloadURL https://gist.github.com/harrisonhjones/4568e192ed5723aba02f35c2153e63e9/raw/publishEventParticleConsole.user.js | |
// @description Adds a "Publish Event" link to the Particle Console logs page. | |
// @author Harrison Jones (harrison@hhj.me) | |
// @match https://console.particle.io/* | |
// @grant none | |
// ==/UserScript== | |
// Changelog | |
// 1.5 - Jan 5th, 2017 - "Publish Event" link added to every console.particle.io page. | |
// 1.4 - Jan 5th, 2017 - Fixed publishEvent triggering on every link, not just the "Publish Event" link | |
// 1.3 - Jan 5th, 2017 - Testing the TamperMonkey update feature. | |
// 1.2 - Jan 5th, 2017 - Added downloadURL annotation. | |
// 1.1 - Jan 5th, 2017 - Testing the TamperMonkey update feature. | |
// 1.0 - Jan 5th, 2017 - Initial Release. | |
(function() { | |
'use strict'; | |
var getCookies = function(){ | |
var pairs = document.cookie.split(";"); | |
var cookies = {}; | |
for (var i=0; i<pairs.length; i++){ | |
var pair = pairs[i].split("="); | |
cookies[pair[0].trim()] = unescape(pair[1]); | |
} | |
return cookies; | |
}; | |
var getAccessToken = function() { | |
var cookies = getCookies(); | |
var token = JSON.parse(cookies.ember_simple_auth_session).authenticated.access_token; | |
return token; | |
}; | |
$(window).on('load', function (e) { | |
$('.right').prepend('<div class="secondary-info docs" id="publishEvent"><a href="#"><i class="ion-paper-airplane"></i><span>Publish Event</span></a></div>'); | |
$('#publishEvent').click(function(){ | |
var eventName = prompt("Please enter an event name", "test event"); | |
if (eventName !== null) { | |
var eventData = prompt("Please enter the event data", "test event data"); | |
var url = "https://api.particle.io/v1/devices/events"; | |
var data = { | |
name: eventName, | |
data: eventData, | |
access_token: getAccessToken(), | |
private: true | |
}; | |
var jqxhr = $.post(url, data, function() { | |
// alert( "success" ); | |
}) | |
.done(function() { | |
alert( "Success\nEvent published" ); | |
}) | |
.fail(function() { | |
alert( "Error\nUnable to publish event. Perhaps your access token has expired?" ); | |
}); | |
/*.always(function() { | |
alert( "finished" ); | |
});*/ | |
} else { | |
alert("Error\nYou must enter an eventName"); | |
} | |
return false; | |
}); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment