使用类似 Greasemonkey 的脚本自动选择每页的项目(下拉列表)

Auto select items-per-page (a drop down) using a Greasemonkey-like script

本文关键字:项目 下拉列表 选择 Greasemonkey 脚本      更新时间:2023-09-26

如果你看一下这个页面,有一个下拉菜单来显示每页的结果:它可以是10、20或50。

我想要一个类似 Greasemonkey 的脚本来模拟每页选择 50 个。

为了便于参考,页面中的一些 HTML 如下所示:

<select><option value="10">10</option><option value="20">20</option><option value="50">50</option></select> per page

请问我该如何实现这一点?

编辑

网址更新了 20 多个项目

这是针对 Fluid.app 上的 Greasekit(它很旧,但我仅限于使用它)。布洛克可靠地告诉我,它已经很旧了,不再更新了。(我认为我什至无法使用jQuery)

由于该选择是由jQuery驱动的,因此您可以使用jQuery来更改它并触发所有必要的javascript。

但是你的 Greasemonkey 脚本必须使用注入或@grant none模式,因为你需要触发页面的 javascript 函数。

该示例页面的完整脚本如下所示:

// ==UserScript==
// @name        _Amazon-like store, auto select 50 items per page
// @include     https://dl.dropboxusercontent.com/u/5546881/*
// @grant       none
// ==/UserScript==
//-- $ not defined. Use jQuery
jQuery('#v_pagination_long select').val (50).trigger ('change');



更新:

由于OP并没有真正使用Greasemonkey或像Tampermonkey这样的现代等价物,因此不支持@grant none
这是使用脚本注入的相同脚本,几乎可以在任何浏览器+用户脚本引擎组合上运行:

// ==UserScript==
// @name        _Amazon-like store, auto select 50 items per page
// @include     https://dl.dropboxusercontent.com/u/5546881/*
// @grant       GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
function GM_main () {
    jQuery('#v_pagination_long select').val (50).trigger ('change');
}
addJS_Node (null, null, GM_main);
function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    if (runOnLoad) {
        scriptNode.addEventListener ("load", runOnLoad, false);
    }
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';
    var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}



重要:

  1. 该示例页面最多显示 9 个项目,因此不可能 100% 确定脚本正在执行所需的一切。使用OP的新示例页面,验证脚本是否适用于FF + GM和Chrome + TM。
  2. <select>包装在一个div 中,ID 为 v_pagination_long 。使用它来帮助获得正确的控件。
  3. 该页面使用各种鼠标事件(不是单击),因此可能需要更多有状态的方法(无法确定,请参阅第 1 项)。在这种情况下,演示页面不需要。有关其他页面,请参阅在 AJAX 驱动的站点上选择和激活正确的控件。