Facebook comment cleaner

Cleans Facebook comments of idiotic name-only mentions

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name Facebook comment cleaner
// @namespace http://sharparam.com/
// @description Cleans Facebook comments of idiotic name-only mentions
// @copyright 2014, Sharparam (http://sharparam.com/)
// @license MIT; http://opensource.org/licenses/MIT
// @homepageURL https://github.com/Sharparam/UserScripts
// @supportURL https://github.com/Sharparam/UserScripts/issues
// @include /^https?://(www\.)?facebook.com(/.*)?$/
// @version 1.2.0
// @grant none
// @run-at document-end
// ==/UserScript==

function cleanComments() {
    var comments = document.querySelectorAll('li.UFIComment');
    
    for (var cIdx = 0; cIdx < comments.length; cIdx++) {
        var comment = comments[cIdx];
        var body = comment.querySelector('.UFICommentBody');
        var isValid = false;
        var contents = body.children;
        
        for (var i = 0; i < contents.length; i++) {
            var element = contents[i];
            var name = element.tagName;
            var cls = element.className;
            var text = element.textContent ? element.textContent.trim() : (element.innerText ? element.innerText.trim() : null);
            
            if (name !== 'A' && cls !== 'profileLink')
                isValid = true;
        }
        
        if (!isValid)
            comment.remove();
    }
}

window.onload = function() {
    cleanComments();
    // Create a button the user can use to clean the comments
    var html = '<button style="position: fixed; bottom: 10px; left: 10px;">Clean comments</button>';
    var frag = document.createDocumentFragment();
    var temp = document.createElement('div');
    temp.innerHTML = html;
    var btn = temp.firstChild;
    btn.onclick = function() { cleanComments(); };
    document.body.appendChild(btn);
};