User:Sophisticatedevening/LLMslop.js

mw.loader.using('mediawiki.util', function () {

function runAIDetection() {

const contentBox = document.getElementById("mw-content-text");

if (!contentBox) return;

// Limit scope to only what's before References section

let workingHTML = '';

let collecting = true;

for (const node of contentBox.children) {

if (node.tagName === 'H2' && /references/i.test(node.innerText)) {

collecting = false;

}

if (collecting) {

workingHTML += node.outerHTML || node.textContent;

}

}

// Strip text inside quotes for analysis (not in highlighting)

const textOnly = workingHTML.replace(/(["“”][^"“”]{3,100}?["“”])/g, "");

let flaggedHTML = workingHTML;

const redFlags = [];

const issueIdMap = new Map(); // Maps unique flag messages to CSS class IDs

let issueCounter = 0;

function escapeRegex(str) {

return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');

}

function highlightMatches(regex, message, filterFn = () => true) {

let match;

let matchCount = 0;

const uniqueMatches = new Set();

const issueId = `ai-issue-${issueCounter++}`;

let added = false;

while ((match = regex.exec(textOnly)) !== null && matchCount < 50) {

const matchedText = match[0];

if (!uniqueMatches.has(matchedText) && filterFn(matchedText, match.index, textOnly)) {

uniqueMatches.add(matchedText);

const span = `${matchedText}`;

const safeRegex = new RegExp(escapeRegex(matchedText), "g");

flaggedHTML = flaggedHTML.replace(safeRegex, span);

matchCount++;

added = true;

}

}

if (added) {

issueIdMap.set(message, issueId);

redFlags.push({ message, id: issueId });

}

}

// Heuristics

highlightMatches(/\b(is a well-known|has been recognized for|widely regarded as|in recent years,.*?has grown|it is important to note)\b/gi,

"Generic or promotional phrase");

highlightMatches(/\b(notable|renowned|prestigious|innovative|unique|eminent|groundbreaking|leading)\b/gi,

"Vague promotional adjective (excluding neutral uses)", (match, offset, str) => {

if (match.toLowerCase() === "leading") {

const context = str.slice(offset, offset + 20).toLowerCase();

return !(context.startsWith("leading to") || context.startsWith("leading up to"));

}

return true;

});

highlightMatches(/\b(In conclusion|Moreover|Furthermore|To summarize|Therefore|It is important to note)\b/gi,

"Overused transitional phrase");

highlightMatches(/example\.com|placeholder|loremipsum|fakeurl/gi,

"Placeholder or hallucinated reference");

// Malformed citations

const citationRegex = /\{\{Cite (web|news|book|journal)[^}]*\}\}/g;

let citationMatch;

let citationCount = 0;

while ((citationMatch = citationRegex.exec(flaggedHTML)) !== null) {

const cite = citationMatch[0];

const missing = [];

if (!cite.includes("title=")) missing.push("title");

if (cite.includes("web") && !cite.includes("url=")) missing.push("url");

if (!cite.includes("author=")) missing.push("author");

if (missing.length >= 2) {

const issueId = `ai-issue-${issueCounter++}`;

const span = `${cite}`;

flaggedHTML = flaggedHTML.replace(cite, span);

redFlags.push({ message: `Malformed citation missing ${missing.join(", ")}`, id: issueId });

citationCount++;

}

}

// Replace content safely (before References)

const refIndex = Array.from(contentBox.children).findIndex(el =>

el.tagName === 'H2' && /references/i.test(el.innerText)

);

const tempDiv = document.createElement("div");

tempDiv.innerHTML = flaggedHTML;

while (contentBox.firstChild) {

contentBox.removeChild(contentBox.firstChild);

}

Array.from(tempDiv.childNodes).forEach(n => contentBox.appendChild(n.cloneNode(true)));

if (refIndex >= 0) {

Array.from(contentBox.children)

.slice(refIndex)

.forEach(n => contentBox.appendChild(n.cloneNode(true)));

}

const oldBox = document.getElementById("ai-warning-box");

if (oldBox) oldBox.remove();

const emojis = ["⚠️", "🤖", "👀", "🧠", "📎", "🚨"];

const emoji = emojis[Math.floor(Math.random() * emojis.length)];

const box = document.createElement("div");

box.id = "ai-warning-box";

box.style.padding = "1em";

box.style.marginBottom = "1em";

box.style.fontSize = "14px";

box.style.borderRadius = "8px";

if (redFlags.length > 0) {

box.style.background = "#fff4f4";

box.style.border = "2px solid red";

const listItems = redFlags.map(flag =>

`

  • ${flag.message}
  • `

    ).join("");

    box.innerHTML = `${emoji} AI indicators found:

      ${listItems}
    `;

    } else {

    box.style.background = "#f4fff4";

    box.style.border = "2px solid green";

    box.innerHTML = `✅ ${emoji} No major AI-generation signals detected.`;

    }

    contentBox.prepend(box);

    // Smooth scroll behavior

    document.querySelectorAll(".ai-jump").forEach(link => {

    link.addEventListener("click", function (e) {

    e.preventDefault();

    const targetClass = this.dataset.target;

    const el = document.querySelector(`.${targetClass}`);

    if (el) el.scrollIntoView({ behavior: "smooth", block: "center" });

    });

    });

    }

    function addButtonToToolbar(imageSrc) {

    $('#pt-notifications-notice').after(

    $('

  • ').append(

    $('')

    .append(

    $('')

    .css({

    'padding': '0.3em',

    'cursor': 'pointer'

    })

    .attr('src', imageSrc)

    .click(runAIDetection)

    )

    ).css({

    'opacity': '1',

    'transition': 'opacity 0.5s'

    })

    );

    }

    const style = document.createElement("style");

    style.textContent = `

    .ai-flagged {

    background-color: #fff6a2;

    border-bottom: 1px dotted #c00;

    cursor: help;

    }

    .ai-warning-box a {

    color: #d00;

    text-decoration: underline dotted;

    cursor: pointer;

    }

    `;

    document.head.appendChild(style);

    $(document).ready(function () {

    const yourImageSrc = 'https://upload.wikimedia.org/wikipedia/commons/7/71/OOjs_UI_icon_robot.svg';

    addButtonToToolbar(yourImageSrc);

    });

    });