Javascript Greasemonkey脚本不工作但未显示错误?
在该网站中,左侧链接在同一页面中打开,这给阅读和切换到下一主题带来了不便 一个名为Javascript Greasemonkey脚本不工作但未显示错误?,javascript,jquery,greasemonkey,Javascript,Jquery,Greasemonkey,在该网站中,左侧链接在同一页面中打开,这给阅读和切换到下一主题带来了不便 一个名为Getrelease的函数正在传递与单击的项对应的唯一id,以获取并显示在右侧列中 相关页面、HTML代码段为: <div class="leftrightdiv" id="lreleaseID"> <ul class="link1"> <li style="list-style:none;border-bottom: blue 1px dotted;color
Getrelease
的函数正在传递与单击的项对应的唯一id,以获取并显示在右侧列中
相关页面、HTML代码段为:
<div class="leftrightdiv" id="lreleaseID">
<ul class="link1">
<li style="list-style:none;border-bottom: blue 1px dotted;color:blue;font-size:110%" class="rel">Prime Minister's Office</li>
<!-- <li class="rel" style="border-bottom: teal 1px dotted;cursor:pointer" **onclick='Getrelease(94318)'** >PM's statement at the BRICS Leaders - Africa Dialogue Forum<span style='font-weight:lighter;color: ThreeDDarkShadow;'> ()</span></li> -->
<li onclick="Getrelease(94318)" style="border-bottom: teal 1px dotted;cursor:pointer" class="rel">PM's statement at the BRICS Leaders - Africa Dialogue Forum</li>
<li style="list-style:none;border-bottom: blue 1px dotted;color:blue;font-size:110%" class="rel">Ministry of Finance</li>
<!-- <li class="rel" style="border-bottom: teal 1px dotted;cursor:pointer" onclick='Getrelease(94339)' >Central Board of Excise and Customs (CBEC) Offices to Remain open on 29th, 30th & 31stmarch, 2013 ;<span style='font-weight:lighter;color: ThreeDDarkShadow;'> ()</span></li> -->
<li onclick="Getrelease(94339)" style="border-bottom: teal 1px dotted;cursor:pointer" class="rel">Central Board of Excise and Customs (CBEC) Offices to Remain open on 29th, 30th & 31stmarch, 2013 ;</li>
<!-- <li class="rel" style="border-bottom: teal 1px dotted;cursor:pointer" onclick='Getrelease(94338)' >India and Liechtenstein Sign Tax Information Exchange Agreement (TIEA)<span style='font-weight:lighter;color: ThreeDDarkShadow;'> ()</span></li> -->
<li onclick="Getrelease(94338)" style="border-bottom: teal 1px dotted;cursor:pointer" class="rel">India and Liechtenstein Sign Tax Information Exchange Agreement (TIEA)</li>
<!-- <li class="rel" style="border-bottom: teal 1px dotted;cursor:pointer" onclick='Getrelease(94337)' >Government Committed to bring down the Current Account<span style='font-weight:lighter;color: ThreeDDarkShadow;'> ()</span></li> -->
<li onclick="Getrelease(94337)" style="border-bottom: teal 1px dotted;cursor:pointer" class="rel">Government Committed to bring down the Current Account</li>
</ul>
<br>
</div>
首相府
总理在金砖国家领导人-非洲对话论坛上的讲话
财政部
消费税和海关中央委员会(CBEC)办公室将于29日、30日和30日继续营业;2013年3月31日李>
印度和列支敦士登签署税务信息交换协议(TIEA)
- 政府承诺降低经常账户
我想把上面的代码改成下面的代码,这样在单击该项目时,它将在一个新页面中打开
<div class="leftrightdiv" id="lreleaseID">
<ul class="link1">
<li style="list-style:none;border-bottom: blue 1px dotted;color:blue;font-size:110%" class="rel">Prime Minister's Office</li>
<!-- <li class="rel" style="border-bottom: teal 1px dotted;cursor:pointer" onclick='Getrelease(94318)' >PM's statement at the BRICS Leaders - Africa Dialogue Forum<span style='font-weight:lighter;color: ThreeDDarkShadow;'> ()</span></li> -->
<li onclick="window.open('http://www.pib.nic.in/newsite/erelease.aspx?relid=94318')" style="border-bottom: teal 1px dotted;cursor:pointer" class="rel">PM's statement at the BRICS Leaders - Africa Dialogue Forum</li>
<li style="list-style:none;border-bottom: blue 1px dotted;color:blue;font-size:110%" class="rel">Ministry of Finance</li>
<!-- <li class="rel" style="border-bottom: teal 1px dotted;cursor:pointer" onclick='Getrelease(94339)' >Central Board of Excise and Customs (CBEC) Offices to Remain open on 29th, 30th & 31stmarch, 2013 ;<span style='font-weight:lighter;color: ThreeDDarkShadow;'> ()</span></li> -->
<li onclick="window.open('http://www.pib.nic.in/newsite/erelease.aspx?relid=94339')" style="border-bottom: teal 1px dotted;cursor:pointer" class="rel">Central Board of Excise and Customs (CBEC) Offices to Remain open on 29th, 30th & 31stmarch, 2013 ;</li>
<!-- <li class="rel" style="border-bottom: teal 1px dotted;cursor:pointer" onclick='Getrelease(94338)' >India and Liechtenstein Sign Tax Information Exchange Agreement (TIEA)<span style='font-weight:lighter;color: ThreeDDarkShadow;'> ()</span></li> -->
<li onclick="window.open('http://www.pib.nic.in/newsite/erelease.aspx?relid=94338')" style="border-bottom: teal 1px dotted;cursor:pointer" class="rel">India and Liechtenstein Sign Tax Information Exchange Agreement (TIEA)</li>
<!-- <li class="rel" style="border-bottom: teal 1px dotted;cursor:pointer" onclick='Getrelease(94337)' >Government Committed to bring down the Current Account<span style='font-weight:lighter;color: ThreeDDarkShadow;'> ()</span></li> -->
<li onclick="window.open('http://www.pib.nic.in/newsite/erelease.aspx?relid=94337')" style="border-bottom: teal 1px dotted;cursor:pointer" class="rel">Government Committed to bring down the Current Account</li>
</ul>
<br>
</div>
首相府
- 总理在金砖国家领导人-非洲对话论坛上的讲话
财政部
- 中央消费税和海关委员会(CBEC)办公室将于29日、30日和30日继续营业;2013年3月31日李>
- 印度和列支敦士登签署税务信息交换协议(TIEA)
- 政府承诺降低经常账户
所以写下了这个Greasemonkey脚本:
// ==UserScript==
// @name Press Information Bureau
// @namespace http://userscripts.org/scripts/show/163329
// @description PIB new tab open
// @include http://pib.nic.in*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// @version 2.4
// ==/UserScript==
function replaceOnclick() {
var list = document.getElementById('lreleaseID');
var lists = list.getelementsbytagname('li');
for (i = 0; i < lists.length; i++) {
var Str = lists[i].getAttribute("onclick");
var seconds = Str.indexOf(')');
var url = "http://www.pib.nic.in/newsite/erelease.aspx?relid=" + Str.substring(11, second);
lists[i].setAttribute("onclick", "window.open('" + url + "')");
}
}
/==UserScript==
//@name新闻局
//@名称空间http://userscripts.org/scripts/show/163329
//@description PIB新选项卡打开
//@包括http://pib.nic.in*
//@需要http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
//@version 2.4
//==/UserScript==
函数replaceOnclick(){
var list=document.getElementById('lreleaseID');
var list=list.getelementsbytagname('li');
对于(i=0;i
Greasemonkey脚本正在被注入。但它并没有在输出中做出预期的改变。
javascript使用中的错误是什么?请帮助我。该脚本有多个错误:
replaceOnclick()
,但从不调用它这就是为什么在错误控制台中看不到任何操作和错误的原因(Ctrl-Shift J)getelementsbytagname
,然后将抛出一个错误(异常)。JavaScript区分大小写,正确的函数是getElementsByTagName()
Str
有时为空,因此var seconds=Str.indexOf(')代码>将引发异常并使脚本崩溃。防止这种情况的一种方法是将其使用包装在if(Str){…}
语句中
seconds
有时也是空的var url="http://www.pib.nic.in/newsite/erelease.aspx?relid="+Str.substring(11, second);
使用了错误的名称<代码>秒应为秒
还有一些不完全是错误的项目,但您应该采取不同的做法
http://www.pib.nic.in/newsite/...
。因此,您可能需要添加一个include行来说明:
// @include http://www.pib.nic.in*
@grant
,如果可以避免,请不要使用@grant none
使用上面的项目修复该脚本。或者下面是使用jQuery时的情况:
// ==UserScript==
// @name Press Information Bureau
// @namespace http://userscripts.org/scripts/show/163329
// @description PIB new tab open
// @include http://pib.nic.in*
// @include http://www.pib.nic.in*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant GM_addStyle
// @version 2.4
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
//-- Use jQuery selector to get just the <li>s that have an onclick.
var articleLinks = $("#lreleaseID li[onclick]");
//-- jQuery .each()
articleLinks.each ( function () {
var jThis = $(this); // "this" is a special var inside .each()
var onClickVal = jThis.attr ("onclick");
var articleMatch = onClickVal.match (/Getrelease\s*\((\d+)\)/i);
if (articleMatch && articleMatch.length == 2) {
var articleId = articleMatch[1];
var articleUrl = "http://www.pib.nic.in/newsite/erelease.aspx?relid="
+ articleId
;
jThis.attr ("onclick", "window.open('" + articleUrl + "')");
}
} );
/==UserScript==
//@name新闻局
//@名称空间http://userscripts.org/scripts/show/163329
//@description PIB新选项卡打开
//@包括http://pib.nic.in*
//@包括http://www.pib.nic.in*
//@需要http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
//@grant GM_addStyle
//@version 2.4
//==/UserSc