GreasyFork 一鍵回報垃圾評論

在 Greasy Fork 一鍵回報垃圾評論

目前為 2023-09-19 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        GreasyFork: One Click Report Spam
// @name:zh-TW  GreasyFork 一鍵回報垃圾評論
// @name:zh-CN  GreasyFork 一键回报垃圾评论
// @namespace   UserScripts
// @match       https://greasyfork.org/*
// @grant       none
// @version     1.4
// @author      CY Fung
// @license     MIT
// @description To report spam comments in Greasy Fork with one click
// @description:zh-TW 在 Greasy Fork 一鍵回報垃圾評論
// @description:zh-CN 在 Greasy Fork 一键回报垃圾评论
// ==/UserScript==

(() => {

  const TEST_MODE = 0;
  let skipMode = false;

  const onIframeLoad = async (evt) => {
    const iframe = evt.target;
    if (!(iframe instanceof HTMLIFrameElement)) return;

    if (skipMode) return;


    const onNewUrl = async () => {
      skipMode = true;

      alert('reported');
      await new Promise(requestAnimationFrame);
      iframe.remove();
      skipMode = false;

    }
    const onAbort = async () => {
      skipMode = true;

      await new Promise(requestAnimationFrame);
      iframe.remove();
      skipMode = false;
    }

    iframe.removeEventListener('load', onIframeLoad, false);

    if (!iframe.contentDocument) {
      alert('Iframe Access Error. Action aborted.');
      onAbort();
      return;
    }


    const reportReasonRadio = iframe.contentDocument.querySelector('input[name="report[reason]"]');
    if (reportReasonRadio) {
      reportReasonRadio.scrollIntoView();
      await new Promise(requestAnimationFrame);
      reportReasonRadio.click();
      const form = reportReasonRadio.closest('form');
      let currentUrl = iframe.contentWindow.location.pathname;
      skipMode = true;
      if (TEST_MODE) {

        iframe.contentWindow.location.href = 'https://greasyfork.org/'
      } else {
        form.submit();
      }
      let cid = setInterval(() => {
        if (!cid) return;
        let nextUrl = iframe.contentWindow.location.pathname;
        if (nextUrl !== currentUrl) {
          clearInterval(cid)
          cid = 0;
          setTimeout(onNewUrl, 300);
        }
      }, 100)

    } else if (iframe.contentDocument.querySelector('#open-report-:not(:empty)')) {
      alert("The spam report is already submitted for moderator's review. Action aborted.");
      onAbort();

    } else {
      alert('Cannot find the report[reason] radio button. Action aborted.');
      onAbort();
    }

  };
  const clickHandler = (evt) => {
    evt.preventDefault();
    if (!(evt.target instanceof HTMLElement)) return;
    let url = evt.target.getAttribute('ohref');
    if (!url) return;
    let userid = /id=(\d+)\b/.exec(url);
    if (userid) userid = userid[1];
    let r = window.confirm(`Confirm to report user#${userid || "------"} ?`);
    if (!r) return;
    const iframe = document.createElement('iframe');
    skipMode = false;
    iframe.addEventListener('load', onIframeLoad, false);
    iframe.name = "u423323";
    iframe.src = url;
    Object.assign(iframe.style, {
      display: 'block',
      position: 'fixed',
      top: '0px',
      left: '0px',
      width: '300px',
      height: '300px',
      'contain': 'strict',
    });
    document.body.appendChild(iframe);
  }


  for (const anchor of document.querySelectorAll('a[href*="/reports/new?item_class=comment&item_id="],a[href*="/reports/new?item_class=discussion&item_id="]')) {

    let anchorNode = anchor;
    if (anchor.parentNode.firstElementChild === anchor.parentNode.lastElementChild) {
      anchorNode = anchorNode.parentNode;
    }
    let newAnchorNode = anchorNode.cloneNode(true);
    let newAnchor = newAnchorNode.querySelector('a[href]') || newAnchorNode;
    newAnchor.classList.add('report-spam-btn');
    newAnchor.setAttribute('ohref', newAnchor.getAttribute('href'));
    newAnchor.setAttribute('href', '#');
    newAnchor.addEventListener('click', clickHandler, false)
    newAnchor.textContent = 'Report Spam';
    anchorNode.parentNode.insertBefore(newAnchorNode, anchorNode.nextSibling);


  }

  document.head.appendChild(document.createElement('style')).textContent=`
  .discussion-list-container{
  display:flex;
  flex-direction: row;
  flex-wrap: nowrap;
  }
  .discussion-list-container > .discussion-list-item {
      flex-grow:1;
      width:0;
  }
  .discussion-list-container > .discussion-list-item ~ .report-spam-btn {
      align-self: center;
  }
  `

  setTimeout(()=>{

    for(const li of document.querySelectorAll('.discussion-list-item')){

      let a = li.querySelector('a[href*="/discussions/"].discussion-title');
      if(!a) continue;
      let href = a.getAttribute('href');
      let idx = href.lastIndexOf('/discussions/');
      let discussionId = parseInt(href.substring(idx+'/discussions/'.length)||0);
      if(isNaN(discussionId) || discussionId < 0)continue;

      let btn = document.createElement('a');
      btn.classList.add('report-spam-btn');
      btn.setAttribute('ohref', 'https://greasyfork.org/en/reports/new?item_class=discussion&item_id='+discussionId)
      btn.setAttribute('href','#')
      btn.textContent='Report Spam';
      btn.addEventListener('click', clickHandler, false)
      li.parentNode.insertBefore(btn, li.nextSibling);


    }

  }, 270);

})();