1

Web サイト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 スクリプトが挿入されています。しかし、出力に期待される変更が加えられていません。
JavaScriptの使い方の間違いは何ですか?親切に私を助けてください。

4

1 に答える 1

1

そのスクリプトには複数のエラーがあります:

  1. 定義しますreplaceOnclick()が、呼び出しません。 これが、エラー コンソールにアクションもエラーも表示されない理由です( Ctrl Shift J)。

  2. 無効な関数名 はgetelementsbytagname、エラー (例外) をスローします。JavaScript では大文字と小文字が区別され、正しい関数はgetElementsByTagName().

  3. Strnull の場合があるためvar seconds=Str.indexOf(')');、例外がスローされ、スクリプトがクラッシュします。これを防ぐ 1 つの方法は、その使用をif (Str) {...}ステートメントでラップすることです。

  4. 同様に、secondsnull の場合もあります。

  5. この行:

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

    間違った名前を使用しています。secondする必要がありますseconds


正確にはエラーではありませんが、別の方法で行う必要がある項目がいくつかあります...

  1. そのサイトのデフォルトの URL は likehttp://www.pib.nic.in/newsite/...です。したがって、おそらくそれを考慮して include 行を追加する必要があります。

    // @include         http://www.pib.nic.in*
    
  2. スクリプトは jQuery を参照していますが、使用していません。jQuery を使用すると、時間と労力を節約できます。

  3. スクリプトはディレクティブを指定@grantません。これにより、予期しない動作や競合が発生する可能性があります。常に少なくとも 1@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 + "')");
    }
} );
于 2013-03-29T01:03:24.330 に答える