javascript - 热键用户脚本有效,但在特定站点上除外?

标签 javascript greasemonkey

目标/测试页面: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/';   
            } 
        }
    );
};

最佳答案

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

所有代码都包含在:

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

但是 document.readyState 将始终是交互式,除非指定了 @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/';
    }
} );

关于javascript - 热键用户脚本有效,但在特定站点上除外?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13981393/

相关文章:

javascript - 从 url 中删除查询字符串 - HashRouter

google-chrome - Greasemonkey @require 在 Chrome 中不起作用

javascript - 如何在 Javascript 中设置焦点表行

javascript - 停止 JavaScript 中挂起的异步函数

javascript - 仅显示 javascript 对象的对象属性

javascript - Greasemonkey 脚本,用于将具有特定域的页面上的所有 URL 替换为不同的域

javascript - 使用 javascript 小书签在网页中查找 'old' 的所有实例并将每个实例替换为 'new'

Javascript/JQuery 填充数组许多元素

facebook - 使用异常内容加载时不执行 Greasemonkey 脚本

javascript - 从数字数组中删除一个数字以对数组进行排序