gpt4 book ai didi

javascript - Greasemonkey 脚本无法运行但没有显示错误?

转载 作者:行者123 更新时间:2023-11-28 02:20:48 24 4
gpt4 key购买 nike

在网站pib.nic.in/newsite/erelease.aspx ,左侧链接在同一页面中打开——给阅读和切换到下一个主题带来不便。

一个名为 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 &amp; 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>


我想将上面的代码更改为以下代码,以便单击该项目时,它将在新页面中打开。

<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 &amp; 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>


于是写了这个 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 + "')");
}
}


Greasemonkey 脚本正在被注入(inject)。但它并没有在输出中做出预期的改变。
javascript使用中有什么错误?请帮助我。

最佳答案

该脚本有多个错误:

  1. 它定义了 replaceOnclick() 但从未调用它。 这就是为什么您在错误控制台中看不到任何操作和错误 (Ctrl Shift J)。

  2. 无效的函数名称 getelementsbytagname 将引发错误(异常)。 JavaScript 区分大小写,正确的函数是 getElementsByTagName()

  3. Str 有时为 null,因此 var Seconds=Str.indexOf(')'); 将引发异常并使脚本崩溃。防止这种情况的一种方法是将其使用包装在 if (Str) {...} 语句中。

  4. 同样,有时为空。

  5. 这一行:

    var url="http://www.pib.nic.in/newsite/erelease.aspx?relid="+Str.substring(11, second);

    使用了错误的名称。 应为

<小时/>

还有一些并不完全是错误的项目,但您应该采取不同的做法...

  1. 该站点默认的 URL 类似于 http://www.pib.nic.in/newsite/...。因此,您可能需要添加一个包含行来解决这个问题:

    // @include         http://www.pib.nic.in*
  2. 脚本引用了 jQuery,但没有使用它!使用 jQuery,它将节省您的时间并减少麻烦。

  3. 脚本未指定 a @grant directive 。这可能会导致意外的行为和冲突。请始终指定至少一行 @grant 行,如果可以避免,请勿使用 @grant none

  4. jQuery 1.3.2“Hella”已过时。如果您打算使用 jQuery,请使用更新的版本,除非有令人信服的理由不这样做。

<小时/>

使用上面的项目来修复该脚本。或者这是使用 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 + "')");
}
} );

关于javascript - Greasemonkey 脚本无法运行但没有显示错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15691564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com