Skip to content

Instantly share code, notes, and snippets.

@samhenrigold
Last active July 4, 2021 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samhenrigold/887ebeb947fe0844e6e58e91e2f86d6c to your computer and use it in GitHub Desktop.
Save samhenrigold/887ebeb947fe0844e6e58e91e2f86d6c to your computer and use it in GitHub Desktop.
Useful JS Console Snippets

Check all checkboxes

$$('input[type="checkbox"').map(i => i.checked = true)

UNcheck all checkboxes

$$('input[type="checkbox"').map(i => i.checked = false)

Allow all text to be selectable

document.querySelectorAll('*').style.userSelect = 'auto';

Extract all links from website to a table

for(var x=document.querySelectorAll("a"),myarray=[],i=0;i<x.length;i++){var nametext=x[i].textContent,cleantext=nametext.replace(/\s+/g," ").trim(),cleanlink=x[i].href;myarray.push([cleantext,cleanlink])}function make_table(){for(var t="<table><thead><th>Name</th><th>Links</th></thead><tbody>",e=0;e<myarray.length;e++)t+="<tr><td>"+myarray[e][0]+"</td><td>"+myarray[e][1]+"</td></tr>";window.open("").document.write(t)}make_table();

Auto-scroll on an infinite scroll page

function sleep(ms) {return new Promise(resolve => setTimeout(resolve, ms));} ContinueCycle = true while(ContinueCycle) {await sleep(2000); window.scrollTo(0,document.body.scrollHeight);}

Stop auto-scrolling

ContinueCycle = false

Bookmarklets

Get Twitter poll results without voting

javascript:!( function () { if (window.location.origin !== 'https://twitter.com' || window.location.pathname.split('/').length !== 4) return alert('This is not a valid Twitter poll!'); alert('Please wait this can take up to 30 seconds. Select OK or close to get started. If this is a valid poll, you will get the result when it is ready...:)'); let result = ''; let processDone = false; let oldXHROpen = window.XMLHttpRequest.prototype.open; const startTime = Date.now(); window.XMLHttpRequest.prototype.open = function () { if (processDone) { return oldXHROpen.apply(this, arguments); } function listenEvent() { try { if (this.responseURL.includes('poll')) { const pollResultJson = JSON.parse(this.responseText); const pollBindingValues = pollResultJson.card.binding_values; const resultObj = Object.keys(pollBindingValues).reduce(function (accum, key) { if (key.includes('_label')) { const label = pollBindingValues[key].string_value; const choiceNumber = key.replace(/\D/g, ''); const voteCount = pollBindingValues[`choice${choiceNumber}_count`].string_value; accum.choiceVoteCountMap[label] = +voteCount; accum.noOfVotes += +voteCount; } return accum; }, { noOfVotes: 0, choiceVoteCountMap: {} }); const { choiceVoteCountMap, noOfVotes } = resultObj; Object.keys(choiceVoteCountMap).forEach(function (label) { result += `${label}: ${((choiceVoteCountMap[label] / noOfVotes) * 100).toFixed(2)}% with ${choiceVoteCountMap[label]} votes\n` }) } } catch (e) { processDone = true; if (window.confirm('Something went wrong. Could you please report the issue?')) { window.open('https://www.syncwithtech.org/twitter-poll-result-wo-voting/', '_blank'); } this.removeEventListener('load', listenEvent); } } this.addEventListener('load', listenEvent); if (result) { processDone = true; alert(result); this.removeEventListener('load', listenEvent); } if (Date.now() - startTime > 40000 && !processDone) { processDone = true; if (window.confirm('Seems like it is not a valid Twitter poll. Press OK to report the issue if it is a valid poll and the bookmarklet failed to detect it.')) { window.open('https://www.syncwithtech.org/twitter-poll-result-wo-voting/', '_blank'); } this.removeEventListener('load', listenEvent); } return oldXHROpen.apply(this, arguments); } })();

Enable editing mode

javascript:document.body.contentEditable%20=%20'true';%20document.designMode='on';%20void%200

Create youtube-dl command from current video

javascript:navigator.clipboard.writeText("youtube-dl -i "+Array.from(document.getElementsByTagName('a')).filter(a => a.id == "video-title").map(a => `"${a.href}"`).join(' '));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment