【问题标题】:Userscript to append() on freshly-loaded Facebook timeline elements在新加载的 Facebook 时间线元素上附加()的用户脚本
【发布时间】:2016-12-18 15:25:37
【问题描述】:

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

我的代码:

// ==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!');
});

现在怎么办?

有没有什么方法可以让按钮出现在每个群组帖子上,即使是在这些因向下滚动而加载的帖子上?

【问题讨论】:

  • 最简单的方法是使用间隔计时器继续检查该类并查看您的按钮是否存在于每个类中。除此之外,您还可以使用更复杂的方法,例如使用 MutaionObservers,甚至弄清楚如何挂钩 facebook ajax 代码

标签: javascript jquery facebook userscripts


【解决方案1】:

这些帖子是由 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!');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 2014-09-03
    • 2012-03-30
    • 1970-01-01
    相关资源
    最近更新 更多