【问题标题】:Hotkey userscript works, except on a specific site?热键用户脚本有效,除了在特定站点上?
【发布时间】:2012-12-20 22:12:38
【问题描述】:

目标/测试页面:http://wwwdyn.zdf.de/common/weather/00012.html

这个网站有什么特别之处吗?还是我的代码失败?

热键适用于除天气页面以外的所有网站。所有工作都在非天气if() 声明中完成(隐藏一些东西)。

Cookie 保存天气位置(不是天气页面;主页上的小天气信息)。在体育页面上打开文章预览。

热键应该可以在所有包含的页面上使用。

// ==UserScript==
// @name        heute.de (zdf)
// @version     1.4.2
// @author      unrealmirakulix
// @description optimiert heute.de
// @icon        http://www.heute.de/ZDF/zdfportal/blob/5983926/8/data.png
// @include     http*://www.heute.de/
// @include     http*://www.zdfsport.de/*
// @include     http://wwwdyn.zdf.de/common/weather/00012.html
// @copyright   none
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==

// Handler for .ready() called
if (document.readyState == "complete") {

    // not on weather module
    if ( location.href.indexOf("weather") == -1 ) {
        // hide header + footer
        document.getElementById('header').style.visibility = 'hidden'; // hide #header

        var cf = document.getElementById('footer'); // #footer selection
        cf.style.visibility = 'hidden'; // hide it
        // also hide its children
        if ( cf.hasChildNodes() )   {
            while ( cf.childNodes.length >= 1 )
            {
                cf.removeChild( cf.firstChild );       
            } 
        }

        // different actions @ different subpages
        // ZDF Sport (check if url in address bar contains "zdfsport.de")
        if ( location.href.indexOf("zdfsport.de") > -1 ) {
            $("#a2").click();
            $("#a3").click();
            $("#a4").click();
            $("#a5").click();
        }
        // Heute.de (check if url in address bar contains "heute.de")
        else if ( location.href.indexOf("heute.de") > -1 ) {
            //minimize latest news
            window.document.querySelector("#ncWrapper > #nc > #action_ncMinMax").click();

            // check if variable is stored in cookie
            var loc = $.cookies.get('loc_cookie');
            if ( loc != null) {
                var loc_exists = 1;
            }
            else { var loc_exists = 0; };

            // only ask for location if no location is saved yet [functions inside if work]
            if ( !loc_exists ) {
                var loc = prompt("Wählen Sie den Ort für den Wetterbericht?");
                alert('Sie haben ' + loc + ' als Ort für den Wetterbericht gewählt.');
            };

            $("a:contains('" + loc + "')").click();

            // save to cookie
            $.cookies.set('loc_cookie', loc);
        }
        // Error
        else {
            alert('Die Webseite wurde nicht detektiert - heute_de.user.js');
        };
    }

    // Hotkeys (listen to keyboard input)
    $('html').keypress(
        function(event){

            // exclude SHIFT
            if (event.shiftKey) {
                return;
            }
            // exclude CTRL
            else if (event.ctrlKey) {
                return;
            }
            // exlcude ALT
            else if (event.altKey) {
                return;
            }

            // if inside textarea or input
            else if ('textarea' == event.target.tagName.toLowerCase()) {
                return;
            }
            else if ('input' == event.target.tagName.toLowerCase()) {
                return;
            }

            // if key 'w' is pressed
            else if (event.which == 119){
                // open weather modul
                window.location = 'http://wwwdyn.zdf.de/common/weather/00012.html';
            }
            // if key 's' is pressed
            else if (event.which == 115){
                // open sports section
                window.location = 'http://www.zdfsport.de/ZDFsport-Startseite-4002.html';   
            }
            // if key 'h' is pressed
            else if (event.which == 104){
                // open news section
                window.location = 'http://www.heute.de/';   
            } 
        }
    );
};

【问题讨论】:

    标签: javascript greasemonkey


    【解决方案1】:

    您说该脚本适用于所有网站,但天气页面?!这真是太幸运了,因为它根本不应该起作用。

    所有代码都包含在:

    if (document.readyState == "complete") {
        ... ...
    }
    

    document.readyState 将始终为interactive,除非指定了@run-at document-start,或者对于某些超快速加载页面可能

    无论如何,几乎从不需要在 Greasemonkey 脚本中测试 $(document).ready(),因为这是默认情况下运行的 Greasemonkey 脚本。

    此外,对于该页面,jQuery 与页面的 javascript 冲突。使用@grant 指令将脚本恢复到沙箱(这是所有 Greasemonkey 脚本的好习惯)。

    顺便说一句,您可以使用比 1.3.2 更高版本的 jQuery。 . 1.7.2 版似乎运行良好。

    把它们放在一起,这个脚本就变成了:

    // ==UserScript==
    // @name        heute.de (zdf)
    // @version     1.4.2
    // @author      unrealmirakulix
    // @description optimiert heute.de
    // @icon        http://www.heute.de/ZDF/zdfportal/blob/5983926/8/data.png
    // @include     http://wwwdyn.zdf.de/common/weather/00012.html
    // @include     http*://www.heute.de/
    // @include     http*://www.zdfsport.de/*
    // @copyright   none
    // @require     http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.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.
    */
    
    // not on weather module
    if (location.href.indexOf("weather") == -1) {
        // hide header + footer
        document.getElementById('header').style.visibility = 'hidden'; // hide #header
    
        var cf = document.getElementById('footer'); // #footer selection
        cf.style.visibility = 'hidden'; // hide it
        // also hide its children
        if (cf.hasChildNodes()) {
            while (cf.childNodes.length >= 1) {
                cf.removeChild(cf.firstChild);
            }
        }
    
        // different actions @ different subpages
        // ZDF Sport (check if url in address bar contains "zdfsport.de")
        if (location.href.indexOf("zdfsport.de") > -1) {
            $("#a2").click();
            $("#a3").click();
            $("#a4").click();
            $("#a5").click();
        }
        // Heute.de (check if url in address bar contains "heute.de")
        else if (location.href.indexOf("heute.de") > -1) {
            //minimize latest news
            window.document.querySelector("#ncWrapper > #nc > #action_ncMinMax").click();
    
            // check if variable is stored in cookie
            var loc = $.cookies.get('loc_cookie');
            if (loc != null) {
                var loc_exists = 1;
            } else {
                var loc_exists = 0;
            };
    
            // only ask for location if no location is saved yet [functions inside if work]
            if (!loc_exists) {
                var loc = prompt("Wählen Sie den Ort für den Wetterbericht?");
                alert('Sie haben ' + loc + ' als Ort für den Wetterbericht gewählt.');
            };
    
            $("a:contains('" + loc + "')").click();
    
            // save to cookie
            $.cookies.set('loc_cookie', loc);
        }
        // Error
        else {
            alert('Die Webseite wurde nicht detektiert - heute_de.user.js');
        };
    }
    
    // Hotkeys (listen to keyboard input)
    $('html').keypress ( function (event) {
    
        // exclude SHIFT
        if (event.shiftKey) {
            return;
        }
        // exclude CTRL
        else if (event.ctrlKey) {
            return;
        }
        // exlcude ALT
        else if (event.altKey) {
            return;
        }
    
        // if inside textarea or input
        else if ('textarea' == event.target.tagName.toLowerCase()) {
            return;
        } else if ('input' == event.target.tagName.toLowerCase()) {
            return;
        }
    
        // if key 'w' is pressed
        else if (event.which == 119) {
            // open weather modul
            window.location = 'http://wwwdyn.zdf.de/common/weather/00012.html';
        }
        // if key 's' is pressed
        else if (event.which == 115) {
            // open sports section
            window.location = 'http://www.zdfsport.de/ZDFsport-Startseite-4002.html';
        }
        // if key 'h' is pressed
        else if (event.which == 104) {
            // open news section
            window.location = 'http://www.heute.de/';
        }
    } );
    

    【讨论】:

    • 谢谢。我用// Hotkeys (listen to keyboard input) document.onkeydown = function(evt) { evt = evt || window.event; // ... } 替换了关键监听器,现在它可以工作了。
    【解决方案2】:

    页面上存在 JavaScript 错误。我不知道它们是否相关,但通常,错误会阻止其他行为正确执行。

    “未捕获的引用错误:_etc 未定义。” "未能加载资源http://code.etracker.com/t.js?et=GrKHKx"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 2015-07-10
      • 1970-01-01
      • 2017-07-05
      • 2015-12-08
      • 2021-11-11
      • 2014-05-20
      相关资源
      最近更新 更多