returnAvatarLike

28.08.2024, 12:12

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        returnAvatarLike
// @namespace   Violentmonkey Scripts
// @match       https://dtf.ru/*
// @grant       none
// @version     1.0.1
// @author      apchi
// @description 28.08.2024, 12:12
// @license MIT
// ==/UserScript==
let gF;

Object.defineProperty(window, 'gF', {
  get() {
    return this._gF;
  },
  set(value) {
    this._gF = value;
    if (value) {
    updateNotifications('.notification-item--compact')
   updateNotifications('.notification-item--default')

    }
  }
});

const originalFetch = window.fetch;
window.fetch = async (input, options) => {
  if (input.includes("https://api.dtf.ru/v2.1/subsite/me/updates")) {
    const res = await originalFetch(input, options);
    window.gF = await res.clone().json();
    console.log(window.gF);
    return res;
  }
  return originalFetch(input, options);
};

async function updateNotifications(selector) {
  const reg = new RegExp(['ответил','оценил','прокоммент','опубликовал'].join("|"))
  const listEl = await waitForElm(selector);
  listEl.forEach((el, i) => {
    const imageContainer = el.querySelector(".notification-item__image");
    const textContainer = el.querySelector(".notification-item__text");
    const textHtml = textContainer.innerHTML
    if (imageContainer && textContainer) {
      const user = window.gF.result[i]?.users[0];

      if (!reg.test(textHtml)) {
        textContainer.innerHTML = ` <b>${user?.name}</b> оценил ❤️ ${textHtml}`;
      }

      imageContainer.querySelector("svg")?.remove();

      if (!imageContainer.querySelector("img") && user?.avatar_url) {
        const src = user.avatar_url;
        imageContainer.insertAdjacentHTML("beforeend", `
          <div data-loaded="true"
               class="andropov-media andropov-media--rounded andropov-media--bordered andropov-media--has-preview andropov-image"
               style="aspect-ratio: 1 / 1; width: 36px; height: 36px; max-width: none; --background-color: #dddddd;">
            <picture>
              <source srcset="${src}-/scale_crop/72x72/-/format/webp, ${src}-/scale_crop/72x72/-/format/webp 2x" type="image/webp" />
              <img src="${src}-/scale_crop/72x72/" srcset="${src}-/scale_crop/72x72/, ${src}-/scale_crop/72x72/ 2x" alt="" loading="lazy" />
            </picture>
          </div>
        `);
      }
    }
  });
}

window.onload = () => {
  document.querySelector(".bell__button").addEventListener("click", () => {
    updateNotifications('.notification-item--compact');
  });
  updateNotifications('.notification-item--default');
};

function waitForElm(selector) {
  return new Promise((resolve) => {
    const elements = document.querySelectorAll(selector);
    if (elements.length) {
      resolve(elements);
    } else {
      const observer = new MutationObserver(() => {
        const updatedElements = document.querySelectorAll(selector);
        if (updatedElements.length) {
          observer.disconnect();
          resolve(updatedElements);
        }
      });
      observer.observe(document.body, { childList: true, subtree: true });
    }
  });
}