Last active
June 14, 2023 19:07
-
-
Save twilight-sparkle-irl/2d34d4947062321a9f7fb2aee126a11a to your computer and use it in GitHub Desktop.
view poll percentages on tumblr (for pasting into JS console)
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
// get randomly generated string for react elements | |
var suffix = Object.keys(document.querySelector('#base-container'))[0].split('$')[1] | |
var reakt = (x)=>`__react${x+'$'+suffix}` | |
// get current clicked-on post (clicked on OP's url on the dashboard, or went to direct www.tumblr.com link) | |
var tlo = document.querySelector('[data-testid="timelinePosts"] [data-id]')[reakt`Fiber`].child.child.child.memoizedProps.timelineObject | |
var poll = tlo.content.filter(x=>x.type=='poll')[0] // get first poll in post | |
// grab the results for the poll | |
tumblr.apiFetch(`/v2/polls/${tlo.blogName}/${tlo.idString}/${poll.clientId}/results`).then(function (results) { | |
let output = [] | |
let total = Object.values(results.response.results).reduce((s,a)=>s+a, 0); // get a sum of all votes | |
poll.answers.forEach(ans => { | |
// pre-process the info | |
// this processes the client-side poll view and uses it to index the results. | |
// you could theoretically do it the other way around. i won't | |
output.push({'text': ans.answerText, 'votes': results.response.results[ans.clientId], 'id': ans.clientId, 'percentage': 100*results.response.results[ans.clientId]/total}) | |
}) | |
// push what we know to an alert() | |
alert(poll.question+"\n\n"+output.map(a => { | |
with(a)return`${text}: ${votes} votes (${percentage>10?percentage.toPrecision(4):percentage.toPrecision(3)}%)` | |
}).join`\n`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment