Last active
April 2, 2021 02:28
-
-
Save L1Q/a34ba1822956b96891c7602c9cdf81f2 to your computer and use it in GitHub Desktop.
Enhances r/Second epxerience with hints and lingering stats
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== | |
// @version 1.6 | |
// @name r/Second helper | |
// @namespace https://gist.github.com/L1Q | |
// ==/UserScript== |
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 r/Second helper | |
// @namespace https://gist.github.com/L1Q | |
// @version 1.6 | |
// @description Enhances r/Second epxerience with hints and lingering stats | |
// @author L1Q | |
// @updateURL https://gist.githubusercontent.com/raw/a34ba1822956b96891c7602c9cdf81f2/script.meta.js | |
// @downloadURL https://gist.githubusercontent.com/raw/a34ba1822956b96891c7602c9cdf81f2/script.user.js | |
// @icon https://i.imgur.com/4nwvAdk.png | |
// @match https://second-api.reddit.com/embed* | |
// @grant none | |
// @run-at document-end | |
// ==/UserScript== | |
const app_name = "r/Second helper" | |
console.log(`${app_name} running`) | |
const hardfail_retry_timeout = 1000 // ms | |
let is_setup = false | |
const helper_state = | |
{voters_trend: [] | |
,votes_lingering: [] | |
,iframe_container: undefined | |
} | |
window.l1q_helper_state = helper_state | |
const getCurrentRoundShadowRoot = () => document | |
.querySelector('[currentround]') | |
.shadowRoot | |
const getVoteElementOptions = () => Array.prototype.slice.call(getCurrentRoundShadowRoot() | |
.querySelector('#wrapper > [round]').shadowRoot.querySelectorAll('faceplate-form > fieldset > label > button')) | |
const getCurrentVoted = () => | |
getVoteElementOptions().findIndex( (e) => | |
e.className.split(' ').includes("selected") ) | |
const runner = () => { | |
// NOTE: in case state is not setup yet or reddit changed something, retry silently | |
try { | |
const state = JSON.parse(getCurrentRoundShadowRoot() | |
.querySelector('#wrapper > [round]') | |
.getAttribute('round') | |
|| "{}") | |
if(!is_setup) { | |
console.log(`${app_name} is setting up`) | |
/* | |
const is_page_accessible = window.top == window.self | |
console.log(window.top, window.self) | |
if(is_page_accessible) { | |
console.log('page_accessible') | |
const topdoc = window.top.document | |
const iframe_elem = topdoc.querySelector('iframe[data-testid=second-embed]') | |
helper_state.iframe_container = iframe_elem.parentElement | |
window.top.window.l1q_helper_state = helper_state | |
const overlay_elem = topdoc.createElement('div') | |
overlay_elem.className = iframe_elem.className | |
helper_state.iframe_container.insertBefore(overlay_elem, iframe_elem) | |
} | |
*/ | |
is_setup = true | |
console.log(`${app_name} set up successfuly`) | |
} | |
const isInProgress = state.hasOwnProperty("status") | |
&& state.status == "in_progress" | |
if(isInProgress) { | |
// do in progress things | |
// ? press button for the user | |
// record voting distribution | |
// update overlays | |
const time_remaining = state.secondsLeft || 0 | |
const time_total = state.secondsTotal || 0 | |
} else { | |
// do intermission things | |
// record total voters trend | |
// record current result (win/lost) | |
// update overlay with progress (current streak, score graph) | |
if(!helper_state.voters_trend[state.id]) { | |
helper_state.voters_trend[state.id] = state | |
.images.map( i => i.votes) | |
.reduce( (a, b) => a + b ) | |
console.table(helper_state.voters_trend) | |
} | |
} | |
requestAnimationFrame(runner) | |
// reschedule itself, probably with requestAnimationFrame | |
} catch (e) { | |
console.log(`${app_name} failed to init, restarting in ${hardfail_retry_timeout}ms :`, e) | |
setTimeout(runner, hardfail_retry_timeout) | |
} | |
} | |
setTimeout(runner) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment