Telegram Ad Filter

Collapses messages that contain words from the ad-word list

Verzia zo dňa 21.03.2019. Pozri najnovšiu verziu.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

// ==UserScript==
// @name        Telegram Ad Filter
// @version     0.2.2
// @description Collapses messages that contain words from the ad-word list
// @license     MIT
// @author      VChet
// @namespace   Telegram-Ad-Filter
// @include     https://web.telegram.org/*
// @grant       GM_addStyle
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_registerMenuCommand
// ==/UserScript==
"use strict";

const defaultList = [
  "#взаимопиар",
  "#партнерский",
  "#постпроплачен",
  "#реклама",
  "#рекламныйпост",
  "#текстприслан",
];

let messages;
let messagesLength;
let adWords = GM_getValue("ad-words", defaultList);
let delay = GM_getValue("update-interval", 3000);

const applyStyles = messages => {
  // console.log({ adWords });
  messages.forEach(message => {
    message = message.querySelector(".im_message_body");
    if (message.innerText && adWords.some(v => message.innerText.toLowerCase().indexOf(v.toLowerCase()) >= 0)) {
      message.classList.add("advertisementMessage");
      message.onclick = () => message.classList.toggle("advertisementMessage");
    } else {
      message.classList.remove("advertisementMessage");
      message.onclick = null;
    }
  });
};

let eventTimeout;
const eventThrottler = delay => {
  if (!eventTimeout) {
    eventTimeout = setTimeout(() => {
      eventTimeout = null;
      messages = document.querySelectorAll(".im_history_message_wrap");
      if (messages.length === messagesLength || messages.length === 0) return;
      messagesLength = messages.length;
      // console.log({ messagesLength });
      applyStyles(messages);
    }, delay);
  }
};

GM_addStyle(`
  .advertisementMessage {
    max-height: 40px;
  }
  .advertisementMessage > div:before {
    color: dodgerblue;
    font-weight: bold;
    text-decoration: underline dotted;
    content: "Advertisement";
  }
`);

GM_registerMenuCommand("Filter list", () => {
  const wordList = GM_getValue("ad-words", adWords);
  let val = prompt("Enter words to filter, separated by comma:", wordList);
  if (val !== null && typeof val === "string") {
    // Convert string to array, remove empty entries, trim values
    val = val.split(",").filter(v => v).map(v => v.trim());
    adWords = val;
    GM_setValue("ad-words", val);
    messages = document.querySelectorAll(".im_history_message_wrap");
    applyStyles(messages);
  }
});

GM_registerMenuCommand("Update interval", () => {
  const updateInteval = GM_getValue("update-interval", delay);
  const val = prompt("Enter message scanning frequency (in ms):", updateInteval);
  if (val !== null && typeof val === "string") {
    delay = val;
    GM_setValue("update-interval", val);
  }
});

// Run the script when an API message is received, throttled with delay
window.addEventListener("message", () => eventThrottler(delay), false);