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