在新加载的 Facebook 时间轴元素上添加 append() 的用户脚本

Userscript to append() on freshly-loaded Facebook timeline elements

我正在编写一个用户脚本来帮助 Facebook 群组的管理员直接在 post 上执行多项操作。它的目的是添加按钮来禁止用户,删除 post 作者姓名附近的 posts 等(浏览组 posts 时)。我已经使用 jQuery 的 append() 函数编写了在用户名附近添加按钮的代码并且它正在工作 - 它将按钮添加到前 14 个人 ,但是当向下滚动时,Facebook 会加载 posts 的其余部分并且没有按钮。

我的代码:

// ==UserScript==
// @name              admin addon
// @namespace         http://facebook.com/
// @version           1.0
// @description       an addon that makes you perform admin actions with an ease
// @author            mhwq
// @match             https://www.facebook.com/groups/1734441496799901/*
// @require           http://code.jquery.com/jquery-latest.js
// @require           http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js
// @grant             GM_addStyle
// @grant             GM_getResourceText
// ==/UserScript==

$(document).ready(function() {
    $( ".fwb.fcg" ).append(' here go the buttons!');
});

现在怎么办?

有什么方法可以让按钮出现在 每个组 post 上,即使是在这些组上,也是由于向下滚动而加载的?

这些帖子由 AJAX 加载,因此您需要使用 AJAX 感知技术,例如 MutationObserverwaitForKeyElements()

使用 waitForKeyElements,您的脚本变为:

// ==UserScript==
// @name            Facebook admin addon
// @description     an addon that makes you perform admin actions with an ease
// @author          mhwq
// @match           https://www.facebook.com/groups/1734441496799901/*
// @require         http://code.jquery.com/jquery-latest.js
// @require         http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js
// @require         https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant           GM_addStyle
// @grant           GM_getResourceText
// ==/UserScript==

waitForKeyElements (".fwb.fcg", appendButtons);

function appendButtons (jNode) {
    jNode.append ('Here are the buttons!');
}