2013-03-28 57 views
1

在網站pib.nic.in/newsite/erelease.aspx中,左側鏈接在同一頁面中打開 - 給閱讀和切換下一個主題帶來不便。Greasemonkey腳本無法正常工作,但顯示沒有錯誤?

一個名爲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腳本注入得到。但它沒有對產出做出預期的改變。
JavaScript使用中的錯誤是什麼?請幫助我。

回答

1

該腳本中有多處錯誤:

  1. 它定義replaceOnclick()但從未調用它。 這就是爲什麼你看到的錯誤控制檯按CtrlĴ)沒有動作,沒有錯誤。

  2. 無效的函數名稱getelementsbytagname將引發錯誤(異常)。 JavaScript區分大小寫,正確的功能是getElementsByTagName()

  3. Str有時爲空,所以var seconds=Str.indexOf(')');會拋出異常並崩潰腳本。防止這種情況的一種方法是將其用於if (Str) {...}聲明中。

  4. 同樣,seconds有時爲空。

  5. 這條線:

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

    使用錯誤的名字。 second應該是seconds


有些多個項目並不完全錯誤,但你應該做不同的...

  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 + "')"); 
    } 
}); 
+0

非常感謝Brock Adams。如你所說,我會盡力修復我的js。我對jQuery很陌生。當加載頁面http://www.pib.nic.in/newsite/erelease.aspx?relid=0時,您給出的代碼工作得非常好。但是,當我選擇日期,月份,年份來查看左側頂部的舊歸檔文件時,請點擊鏈接後的鏈接 –

+0

Brock Adams非常感謝。我會做。 –

+0

我試圖找出,下拉更改後,值通過WebForm_DoCallback函數傳遞以獲取新內容。但無法找到一個解決方案,在它之後觸發相同的jQuery。我已經在鏈接http://stackoverflow.com/questions/15703056/firing-greasemonkey-script-after-ajax-loading尋求幫助。如果你能解決,將是一個很大的幫助。 –

相關問題