Wait for it

Wait for something. Silently reloads the page every X seconds until some condition is met

当前为 2014-07-05 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name           Wait for it
// @namespace      http://userscripts.org/scripts/show/128187
// @description    Wait for something. Silently reloads the page every X seconds until some condition is met
// @version        1.02
// @include        *
// ==/UserScript==

GM_registerMenuCommand('Wait for something...', function(){

	var search = prompt('Wait for what?\n'+
		'Can be some literal text in the page, a regular expression, CSS selector or XPath query\n'+
		'Leave blank to wait for any change',
		localStorage['wfi_wfw'] || '');
	if(search === null) return;

	var html = null;
	try{ var re = new RegExp(search, 'i'); }
	catch(e){ re = null; }

	function check(doc){
		if(!search){
			if(doc == document) return false;
			var d = doc.documentElement.innerHTML;
			if(!html) html = d;
			return html != d;
		}

		//CSS
		try{ if(doc.querySelector(search)) return true; }
		catch(e){}

		//XPath
		try{
			var res = doc.evaluate(search, doc, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null);
			if(res.singleNodeValue) return true;
		}
		catch(e){}

		//RegExp
		if(re && re.test(doc.documentElement.textContent)) return true;

		//text
		return doc.documentElement.textContent.indexOf(search) > -1;
	}

	if(check(document)){
		alert('"'+search+'" is already there, nothing to wait for...');
		return;
	}

	var secs = prompt('Check every how many seconds?', localStorage['wfi_secs'] || 30)*1;
	if(!secs) return;

	localStorage['wfi_wfw'] = search;
	localStorage['wfi_secs'] = secs;

	var div = document.createElement('div');
	div.innerHTML = 'Waiting for '+(search ? '"'+search+'"' : 'anything')+' every '+secs+' seconds... Last checked at <span id="wfi_last"/>';
	div.style.position = 'fixed';
	div.style.top = '0';
	div.style.left = '0';
	div.style.backgroundColor = '#ffc';
	document.body.appendChild(div);
	var last = document.getElementById('wfi_last');

	var xhr = new XMLHttpRequest();
	function load(){
		document.body.removeChild(div);
		if(check(document)){
			alert((search ? '"'+search+'"' : 'somehing')+' found in '+document.title);
		}
		else{
			document.body.appendChild(div);
			
			xhr.open('GET', document.location.href, true);
			xhr.send();
			last.innerHTML = new Date();
		}
	}

	var doc = document.implementation.createHTMLDocument('');
	xhr.onreadystatechange = function(){
		if (xhr.readyState == 4 && xhr.status == 200){
			doc.documentElement.innerHTML = xhr.responseText;
			if(check(doc)){
				alert((search ? '"'+search+'"' : 'somehing')+' found in '+document.title);
				document.location.reload();
			}
			else setTimeout(load, secs*1000);
		}
	};

	load();
});