Created
November 25, 2016 17:24
-
-
Save jakob-stoeck/3ce4c8674bc4670d262e24c01846c0f4 to your computer and use it in GitHub Desktop.
Export Parse Push Events
This file contains hidden or 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
// Export Parse Push Events | |
// log into http://parse.com and run this code in your web console | |
// the pushes will be inside window.pushes | |
(function() { | |
const parseApp = '[YOUR_PARSE_APP_NAME]'; // the string in the URL https://parse.com/apps/[PARSE_APP]/ e.g. 'push-tests--2' | |
const entries = 10; | |
'use strict'; | |
function get(theUrl) { | |
return new Promise( | |
(resolve, reject) => { | |
let xmlHttp = new XMLHttpRequest(); | |
xmlHttp.onreadystatechange = function() { | |
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) | |
resolve(xmlHttp.responseText); | |
} | |
xmlHttp.open('GET', theUrl, true); | |
xmlHttp.send(null); | |
} | |
); | |
} | |
/** | |
* Parse HTML table into pushes like this: | |
* var pushExample = { | |
* id: 'tFe0qqtBeK', | |
* channels: 'channel1channel2', | |
* content: 'content', | |
* time: '2016-11-24T21:55:30Z' | |
* }; | |
* @param DOMdocument doc | |
* @return Object[] | |
*/ | |
function _parsePushes(doc) { | |
return Array.prototype.slice.call(doc.querySelectorAll('#push_table tr[data-href]')).map(tr => ({ | |
id: tr.querySelector('.pushes_sent').id.replace('pushes_sent_',''), | |
channels: tr.querySelector('.push_target .tip').textContent.trim(), | |
content: tr.querySelector('.push_name').textContent.trim(), | |
time: tr.querySelector('.push_time').textContent.trim(), | |
sent: null | |
})); | |
} | |
function _getSentByPushes(pushes) { | |
return new Promise( | |
(resolve, reject) => { | |
let query = encodeURI(pushes.map((p) => `pushes[${p.id}]=${p.time.replace('Z', '+00:00')}`).join('&')); | |
let url = `https://parse.com/apps/${appName}/push_notifications/pushes_sent_batch?${query}`; | |
console.info('gettingPushes', url); | |
get(url).then(json => resolve(JSON.parse(json))); | |
} | |
); | |
} | |
function getPushes(appName, pageNo) { | |
return new Promise( | |
(resolve, reject) => { | |
let parser = new DOMParser(); | |
let pages = []; | |
for (let i=1; i<=pageNo; i++) { | |
pages.push(new Promise( | |
(resolve, reject) => { | |
console.info(`get page ${i}`); | |
get(`https://parse.com/apps/${appName}/push_notifications?page=${i}&type=all`).then(html => { | |
// add sent amount to pushes | |
let tmpPushes = _parsePushes(parser.parseFromString(html, 'text/html')); | |
_getSentByPushes(tmpPushes).then(sentPushes => { | |
let newPushes = tmpPushes.map(p => Object.assign(p, {sent: sentPushes[p.id]})); | |
resolve(newPushes); | |
}); | |
}); | |
} | |
)); | |
} | |
Promise.all(pages).then(values => { | |
resolve([].concat.apply([], values)); | |
}); | |
} | |
) | |
} | |
window.pushes = []; | |
getPushes(parseApp, Math.ceil(entries/10)).then(result => { | |
pushes = result.slice(0, entries); | |
console.info('your pushes are ready inside window.pushes'); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment