LoginSignup
2
2

More than 3 years have passed since last update.

【某アニメ完結編のネタバレ回避もこれでOKかも】ScriptAutoRunnerを使って、Twitterページを書き換えて特定ワードを含む記事を非表示にする。

Last updated at Posted at 2021-03-11

まいどまいどのScriptAutoRunnerです。

導入方法はこちらご参考ください。ありがたいです。

Amazonの商品ページから、「人気のインディーズマンガ」を消したい(chrome) - Qiita
https://qiita.com/iteyan/items/78bcad46ae43616eeb0f

ということで、今回は、Twitterの画面から、エバー(みさとさん風)という単語が含まれた記事を全消ししてしまいましょう。ネタバレされたら嫌ですもんね。

参考にさせていただいたのは、こちらです。

【某アニメ完結編のネタバレ回避もこれでOK】CSSを使ってTwitterの表示をカスタマイズする - Qiita
https://qiita.com/k_nulluo/items/069700427cb09733f15e

わたし、Twitterやってないのでよくわからないのですが、このように記載したら消えました。


console.log('ScriptAutoRunner twitterHome.js 2021/03/11(Thu)', location.href);
(() => {
  const targetUrls = [
    'twitter.com/home',
  ];
  if (targetUrls.some(url => location.href.includes(url))) {
    console.log('twitter home site !');

    const twitterHome = () => {
      const ngKeyWordArray = [
        'エヴァ',
        'EVA',
        'eva',
      ];

      const articleList = document.querySelectorAll("a");
      for (const element of Array.from(articleList)) {
        if (!element) { continue; }
        const target = element?.parentNode?.parentNode?.parentNode?.parentNode?.parentNode?.parentNode
        if (!target) { continue; }
        const text = target.textContent;
        if (!text) { continue; }
        let deleteFlag = false;
        for (const ngKeyWord of ngKeyWordArray) {
          if (text.includes(ngKeyWord)) {
            // console.log('ScriptAutoRunner', 'Hit', text.replace(/\s+/g, ""));
            console.log('ScriptAutoRunner', 'Hit', ngKeyWord);
            deleteFlag = true;
            break;
          }
        }
        if (deleteFlag) {
          const target1 = target?.parentNode?.parentNode;
          if (!target1) { continue; }
          if (target1.getAttribute('data-testid') === 'tweet') {
            target1.style.display = 'none';
            // target1.remove();
            continue;
          }
          continue;
        }
      }

    };

    setInterval(twitterHome, 1000);

  }
})();

twitterのhome画面のみ対応です。
通知画面で消したい人は、、、、がんばってみてください。

では。

追記

改良しました。
twitterのHome画面だけでなく全体に処理を行うのと、また、通知画面の対応も行いました。

console.log('ScriptAutoRunner twitter.js 2021/03/13(Sat)', location.href);
(() => {
  const targetUrls = [
    'twitter.com/',
  ];
  if (targetUrls.some(url => location.href.includes(url))) {
    console.log('twitter home site !');

    const isFunction = (value) => typeof value === 'function';

    const twitterHome = () => {
      const ngKeyWordArray = [
        'エヴァ',
        'EVA',
        'eva',
      ];

      const articleList = document.querySelectorAll("a");
      for (const element of Array.from(articleList)) {
        if (!element) { continue; }
        const target = element?.parentNode?.parentNode?.parentNode?.parentNode?.parentNode?.parentNode
        if (!target) { continue; }
        const text = target.textContent;
        if (!text) { continue; }
        let deleteFlag = false;
        for (const ngKeyWord of ngKeyWordArray) {
          if (text.includes(ngKeyWord)) {
            // console.log('ScriptAutoRunner', 'Hit', text.replace(/\s+/g, ""));
            console.log('ScriptAutoRunner', 'Hit', ngKeyWord);
            deleteFlag = true;
            break;
          }
        }
        if (deleteFlag) {
          const upLevel = (target, level) => {
            let result = target;
            for (let i = 1; i <= level; i += 1) {
              if (!result.parentNode) {
                break;
              }
              result = result.parentNode;
            }
            return result;
          }
          let target1 = element;
          for (let i = 1; i <= 8; i += 1) {
            target1 = upLevel(element,i);

            if (!target1) { continue; }
            if (!isFunction(target1.getAttribute)) { continue; }
            if (target1.getAttribute('data-testid') === 'tweet') {
              // Homeやその他画面での処理
              target1.style.display = 'none';
              // target1.remove();
              break;
            } else if (target1.getAttribute('role') === 'article') {
              // 通知画面用の処理
              target1.style.display = 'none';
              // target1.remove();
              break;
            }
          }

          continue;
        }
      }

    };

    setInterval(twitterHome, 1000);

  }
})();

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2