2015-12-13 27 views
1

我想自动点击网页上的切换链接。我无法修改其他用户脚本,因为此网页似乎以不同的方式切换。如何在网页上切换此链接?

网页为anidb.net/perl-bin/animedb.pl?show=animelist,切换为右侧栏中“保存/加载设置”旁边的按钮(如果您已登录,或者如果您未登录,或在“重置”旁边)。

下面的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的方法,但无法让它们工作。

+1

$( '一[标题= “菜单的切换显示”]')点击()。 –

回答

0

该切换控件是由javascript(jQuery)添加的,因此您必须使用支持AJAX的技术来访问它。
Choosing and activating the right controls on an AJAX-driven site

您指出的链接有一个CSS类(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); 
}