Loading...
More JavaScript Posts
const union = (...arr) => [...new Set(arr.flat())];// Exampleunion([1, 2], [2, 3], [3]); // [1, 2, 3]
//Retries maxRetries number of times, meaning that if you have 0, then it'll run the function once.async function retryPromise(promiseFn, maxRetries = 3, delayOffset = 0, delayRandomRange = 0) {return new Promise((resolve, reject) => {promiseFn().then(resolve, (error) => { //On errorif (maxRetries <= 0) {return reject(error);} else {if(delayRandomRange * Math.random() + delayOffset < 1.0){return retryPromise(promiseFn, maxRetries - 1, delayOffset, delayRandomRange).then(resolve, reject);}else{return new Promise((resolveTwo, rejectTwo) => {setTimeout(() => {return retryPromise(promiseFn, maxRetries - 1, delayOffset, delayRandomRange).then(resolveTwo, rejectTwo);}, delayRandomRange * Math.random() + delayOffset);}).then(resolve, reject);}}});});}//Tests for that function.//Returns a function that will fail numTimes times, then will return aValuefunction functionThatReturnsAFunctionThatFailsACoupleTimes(numTimes, aValue){return async function(){if(numTimes == 0){return aValue;}numTimes--;throw false;};}const SMALL_NUMBER = 10;function testTest(failNumberOfTimes){let functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(failNumberOfTimes, true);//Test my testfor(let i = 0; i < (failNumberOfTimes * 2); ++i){function evalTest(val){let testTestFailedReason = "";if(val === undefined){testTestFailedReason = "Test test returned undefined for some reason.";}if((i + 1) > failNumberOfTimes){if(val === true){//We're good}else{testTestFailedReason = "Test test didn't return true when it should have.";}}else{if(val === false){//We're good}else{testTestFailedReason = "Test test didn't return false when it should have.";}}testTestFailedReason = testTestFailedReason || "Test test passed test case";console.log(testTestFailedReason, "at index", i, "where the function returned", val);}functionThatFailsACoupleTimes().then(evalTest, evalTest)};}testTest(SMALL_NUMBER);let testCaseCounter = 1;const throwsNoError = [(val)=>{if(val == true){console.log("Passed test case " + (testCaseCounter++));}else{console.error("Unexpected return value", val);}},()=>{console.error("It wasn't supposed to fail!")}]const throwsError = [(val)=>{console.error("It wasn't supposed to succeed!", val);},(val)=>{if(val == false){console.log("Passed test case " + (testCaseCounter++));}else{console.error("Unexpected return value", val);}}];//Runs SMALL_NUMBER times, because SMALL_NUMBER - 1 is the number of retries after the first onelet functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(SMALL_NUMBER - 1, true);retryPromise(functionThatFailsACoupleTimes, SMALL_NUMBER - 1).then(...throwsNoError)functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(SMALL_NUMBER - 1, true);//Runs SMALL_NUMBER - 1 times, because SMALL_NUMBER - 2 is the number of retries after the first oneretryPromise(functionThatFailsACoupleTimes, SMALL_NUMBER - 2).then(...throwsError)//Testing the delay. You'll have to wait a bit too.functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(SMALL_NUMBER - 1, true);//Runs SMALL_NUMBER times, because SMALL_NUMBER - 1 is the number of retries after the first oneretryPromise(functionThatFailsACoupleTimes, SMALL_NUMBER - 1, 500, 500).then(...throwsNoError)functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(SMALL_NUMBER - 1, true);//Runs SMALL_NUMBER - 1 times, because SMALL_NUMBER - 2 is the number of retries after the first oneretryPromise(functionThatFailsACoupleTimes, SMALL_NUMBER - 2, 500, 500).then(...throwsError)
const copyToClipboard = str => {const el = document.createElement('textarea');el.value = str;el.setAttribute('readonly', '');el.style.position = 'absolute';el.style.left = '-9999px';document.body.appendChild(el);const selected =document.getSelection().rangeCount > 0? document.getSelection().getRangeAt(0): false;el.select();document.execCommand('copy');document.body.removeChild(el);if (selected) {document.getSelection().removeAllRanges();document.getSelection().addRange(selected);}};copyToClipboard('Lorem ipsum'); // 'Lorem ipsum' copied to clipboard.
// promisify(f, true) to get array of resultsfunction promisify(f, manyArgs = false) {return function (...args) {return new Promise((resolve, reject) => {function callback(err, ...results) { // our custom callback for fif (err) {reject(err);} else {// resolve with all callback results if manyArgs is specifiedresolve(manyArgs ? results : results[0]);}}args.push(callback);f.call(this, ...args);});};}// usage:f = promisify(f, true);f(...).then(arrayOfResults => ..., err => ...);
//Allegedly never tested on the twitter websitefunction scrapeScreen(){let articles = Array.from(document.getElementsByTagName("article"));let results = [];for(let tweet of articles){const isAnAd = Array.from(tweet.querySelectorAll("span")).map((e)=>e.textContent).includes("Ad");if(isAnAd){//console.log(tweet, "is an ad. Skipping...");continue;}const userName = tweet.querySelector("[data-testid='User-Name'] > div:nth-child(2) > div > div")?.textContent;const tweetContent = tweet.querySelector("[data-testid='tweetText']")?.textContent;const timeStamp = tweet.querySelector("time")?.getAttribute("datetime");const tweetLink = tweet.querySelector("time")?.parentElement?.getAttribute("href");if((!userName) || (!tweetContent)) continue;results.push({username: userName,tweetText: tweetContent,timeStamp: timeStamp,tweetLink: tweetLink});}return results;}let scraped = scrapeScreen();setInterval(()=>{scraped = scraped.concat(scrapeScreen().filter((tweet)=>{for(let scrapedTweet of scraped){if(scrapedTweet.username == tweet.username && scrapedTweet.tweetText == tweet.tweetText) return false;}return true;}));}, 500); //Scrape everything on the screen twice a secondwindow.scrollIntervalId = setInterval(function(){window.scrollBy(0, 1000);}, 500); //Scroll for me//http://bgrins.github.io/devtools-snippets/#console-save(function(console){console.save = function(data, filename){if(!data) {console.error('Console.save: No data')return;}if(!filename) filename = 'console.json'if(typeof data === "object"){data = JSON.stringify(data, undefined, '\t')}var blob = new Blob([data], {type: 'text/json'}),e = document.createEvent('MouseEvents'),a = document.createElement('a')a.download = filenamea.href = window.URL.createObjectURL(blob)a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)a.dispatchEvent(e)}})(console)setTimeout(()=>{clearTimeout(window.scrollIntervalId);delete window.scrollIntervalId;console.save(scraped, "TwitterScrape" + Date.now() + ".json");}, 60 * 1000 * 20); //Twenty minutes
arr = [] // empty arrayconst isEmpty = arr => !Array.isArray(arr) || arr.length === 0;// ExamplesisEmpty([]); // trueisEmpty([1, 2, 3]); // false