目标/测试页: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/';
}
}
);
};
答案 0 :(得分:1)
页面上有JavaScript错误。我不知道它们是否相关,但经常是错误可以阻止其他行为正确执行。
“未捕获的ReferenceError:_etc未定义。” “无法加载资源http://code.etracker.com/t.js?et=GrKHKx”
答案 1 :(得分:1)
你说这个脚本适用于所有网站,但天气页面?!这很幸运,因为它根本不应该有用。
所有代码都包含在:
if (document.readyState == "complete") {
... ...
}
但document.readyState
始终为interactive
,除非指定@run-at document-start
,或可能用于某些超快速加载页面。
无论如何,在Greasemonkey脚本中几乎从不需要尝试测试$(document).ready()
,因为默认情况下会运行Greasemonkey脚本。
此外,对于该页面,jQuery与页面的javascript冲突。使用@grant
指令将脚本恢复到沙箱(这是所有Greasemonkey脚本的好习惯)。
简而言之,您可以使用jQuery而不是1.3.2的更高版本。 。版本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/';
}
} );