Scott's Recipes Logo

The JavaScript of the Twitter Gods

Pizza courtesy of Pizza for Ukraine!

Donate Now to Pizza for Ukraine

 

I found a post on HackerNews that recommended running this JavaScript to fine tune your Twitter settings by muting certain words and I found it to be the kind of JavaScript that, personally, I’ll never write and I’m just in awe of it. Here’s the source of the words as a gist but I have reproduced it below lest it go away. A helpful individual in the HackerNews thread said something like “Here you go – just run this JavaScript in your developer console in Chrome or another browser you are logged into Twitter with and it will fix you right up. “ And it did!

const delayMs = 2000; // change this if you feel like its running too fast

const keywords = `suggest_grouped_tweet_hashtag
suggest_pyle_tweet
suggest_ranked_organic_tweet
suggest_ranked_timeline_tweet
suggest_recap
suggest_recycled_tweet
suggest_recycled_tweet_inline
suggest_sc_tweet
suggest_timeline_tweet
suggest_who_to_follow
suggestactivitytweet
suggestpyletweet
suggestrecycledtweet_inline`.split(/\W+/);

const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set;

const addMutedKeyword = keyword => {
  const input = document.querySelector("[name='keyword']");
  nativeInputValueSetter.call(input, keyword);
  input.dispatchEvent(new Event('input', { bubbles: true }));
  document.querySelector("[data-testid='settingsDetailSave']").click();
}

const delay = () => {
  return new Promise(res => setTimeout(res, delayMs));
};

keywords.reduce(async (prev, keyword) => {
  await prev;
  document.querySelector("a[href='/settings/add_muted_keyword']").click();
  await delay();
  addMutedKeyword(keyword);
  return delay();
}, Promise.resolve());