LoginSignup
0
2

More than 3 years have passed since last update.

【Twitter】目障りなプロモーションを非表示にするブックマークレット【広告ブロック】

Posted at
javascript:(()=>{let e;window.addEventListener("scroll",()=>{clearTimeout(e),e=setTimeout(()=>{(()=>{const e=document.querySelectorAll("article");for(let t of e)t.textContent.endsWith("プロモーション")&&t.remove()})()},100)})})();

はじめに

ブラウザ版ツイッターを使っています。ホームのタイムラインに表示されるプロモーションが邪魔だったので、これを見えなくするやつをつくりました。

解説

上記コードは以下のコードを圧縮したものです。

(
    () => {
// ---------

const remove_ads = () => {
    const art = document.querySelectorAll("article");
    for (let ele of art) {
        if (ele.textContent.endsWith("プロモーション")) {
            ele.remove();
        }
    }
}

let timeoutId;
// スクロール停止してから0.1秒に、remove_ads()を実行
window.addEventListener("scroll", () => {
    clearTimeout(timeoutId);
    timeoutId = setTimeout(() => {
        remove_ads();
    }, 100);
});

// ---------
    }
)();

簡単に説明すると、ざっと次の通り。

  1. 前半はremove_ads()関数を定義。articleタグを順に見ていき、「プロモーション」という文字列で終わる記事があれば削除する、というもの。
  2. 後半はスクロールイベントの設定。画面がスクロールするたびに、remove_ads()を呼び出す。実行するタイミングはスクロールが止まった直後。回数は1度(ココは重要。無駄な処理をしない)。

補足

  1. twitter.comにいる間は、homeからtwitter.com/exploreなどへ移動してまたhomeへ戻ってきても、このブックマークレットは機能するようです。
  2. 広告主がプロモーションにぶら下がるかたちで記事を繋げる場合には対応していません。
  3. iPhone Chrome、Mac Chromeで確認しました。

あとがき

いつもお引き立て頂き深く感謝します。
本日もありがとうございました。

参考

スクロールイベントに関する親切な記事。

  1. https://lab.syncer.jp/Web/JavaScript/Snippet/46/
0
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
0
2