如何在网页上切换此 link?
How to toggle this link on a webpage?
我想自动点击网页上的开关 link。我无法修改其他用户脚本,因为该网页的切换方式似乎有所不同。
网页是 anidb.net/perl-bin/animedb.pl?show=animelist,开关是一个按钮,在右栏中,在 "Save/Load Settings" 旁边(如果您已登录,或者在 "Reset" 旁边,如果你不是)。
下面的HTML似乎是控制开关的。
<div class="g_menu filter_menu expanded">
<h3>
<a class="i_icon i_expanded" title="Toggle display of menu"></a>
</h3>
我尝试了 another Stack Overflow question 中的一些方法,但无法使它们起作用。
该切换控件由 javascript (jQuery) 添加,因此您必须使用 AJAX 感知技术来访问它。
参见 Choosing and activating the right controls on an AJAX-driven site。
您指出的 link 有一个 CSS class (i_expanded
),它是 waitForKeyElements()
的一个很好的选择器。
因此,该站点的 完整脚本 很简单:
// ==UserScript==
// @name aniDB, auto-close the sidebar menu
// @match *://anidb.net/perl-bin/animedb*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
waitForKeyElements (
".filter_menu.expanded .i_expanded", clickNode, true
);
function clickNode (jNode) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
}
我想自动点击网页上的开关 link。我无法修改其他用户脚本,因为该网页的切换方式似乎有所不同。
网页是 anidb.net/perl-bin/animedb.pl?show=animelist,开关是一个按钮,在右栏中,在 "Save/Load Settings" 旁边(如果您已登录,或者在 "Reset" 旁边,如果你不是)。
下面的HTML似乎是控制开关的。
<div class="g_menu filter_menu expanded">
<h3>
<a class="i_icon i_expanded" title="Toggle display of menu"></a>
</h3>
我尝试了 another Stack Overflow question 中的一些方法,但无法使它们起作用。
该切换控件由 javascript (jQuery) 添加,因此您必须使用 AJAX 感知技术来访问它。
参见 Choosing and activating the right controls on an AJAX-driven site。
您指出的 link 有一个 CSS class (i_expanded
),它是 waitForKeyElements()
的一个很好的选择器。
因此,该站点的 完整脚本 很简单:
// ==UserScript==
// @name aniDB, auto-close the sidebar menu
// @match *://anidb.net/perl-bin/animedb*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
waitForKeyElements (
".filter_menu.expanded .i_expanded", clickNode, true
);
function clickNode (jNode) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
}