~simulacrumparty/latest-tweets

e622cab10be1f02ea063ad93371de613b8fd19ae — Dylan Baker 1 year, 9 months ago 0.0.3
Initial commit
6 files changed, 64 insertions(+), 0 deletions(-)

A .gitignore
A icons/128.png
A icons/16.png
A icons/48.png
A index.js
A manifest.json
A  => .gitignore +1 -0
@@ 1,1 @@
web-ext-artifacts/

A  => icons/128.png +0 -0
A  => icons/16.png +0 -0
A  => icons/48.png +0 -0
A  => index.js +42 -0
@@ 1,42 @@
const switchToLatest = async () => {
  const waitForElements = async (els) => {
    return new Promise(async (resolve, reject) => {
      let counter = 0;
      while (true) {
        if (counter === 100) {
          return reject();
        }

        for (let el of els) {
          if (document.querySelector(el)) {
            return resolve(els.map((el) => document.querySelector(el)));
          }
        }

        await new Promise((resolve, _reject) => requestAnimationFrame(resolve));

        counter += 1;
      }
    });
  };

  const topTweetsOffSelector = '[aria-label="Top Tweets off"]';
  const topTweetsOnSelector = '[aria-label="Top Tweets on"]';
  const latestTweetsSelector = 'div[role="menuitem"]';

  waitForElements([topTweetsOnSelector, topTweetsOffSelector])
    .then((els) => {
      const [topTweetsOn, _topTweetsOff] = els;
      if (topTweetsOn) {
        topTweetsOn.click();
        waitForElements([latestTweetsSelector])
          .then((els) => {
            els.filter((el) => !!el).forEach((el) => el.click());
          })
          .catch(() => {});
      }
    })
    .catch(() => {});
};

switchToLatest();

A  => manifest.json +21 -0
@@ 1,21 @@
{
  "name": "Latest Tweets",
  "version": "0.0.3",
  "manifest_version": 2,
  "description": "Automatically switch back to latest tweets",
  "content_scripts": [
    {
      "matches": [
        "https://twitter.com/home"
      ],
      "js": [
        "index.js"
      ]
    }
  ],
  "icons": {
    "16": "icons/16.png",
    "48": "icons/48.png",
    "128": "icons/128.png"
  }
}