Wait for it

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

  1. // ==UserScript==
  2. // @name Wait for it
  3. // @namespace http://userscripts.org/scripts/show/128187
  4. // @description Wait for something. Silently reloads the page every X seconds until some condition is met
  5. // @version 1.02
  6. // @include *
  7. // ==/UserScript==
  8.  
  9. GM_registerMenuCommand('Wait for something...', function(){
  10.  
  11. var search = prompt('Wait for what?\n'+
  12. 'Can be some literal text in the page, a regular expression, CSS selector or XPath query\n'+
  13. 'Leave blank to wait for any change',
  14. localStorage['wfi_wfw'] || '');
  15. if(search === null) return;
  16.  
  17. var html = null;
  18. try{ var re = new RegExp(search, 'i'); }
  19. catch(e){ re = null; }
  20.  
  21. function check(doc){
  22. if(!search){
  23. if(doc == document) return false;
  24. var d = doc.documentElement.innerHTML;
  25. if(!html) html = d;
  26. return html != d;
  27. }
  28.  
  29. //CSS
  30. try{ if(doc.querySelector(search)) return true; }
  31. catch(e){}
  32.  
  33. //XPath
  34. try{
  35. var res = doc.evaluate(search, doc, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null);
  36. if(res.singleNodeValue) return true;
  37. }
  38. catch(e){}
  39.  
  40. //RegExp
  41. if(re && re.test(doc.documentElement.textContent)) return true;
  42.  
  43. //text
  44. return doc.documentElement.textContent.indexOf(search) > -1;
  45. }
  46.  
  47. if(check(document)){
  48. alert('"'+search+'" is already there, nothing to wait for...');
  49. return;
  50. }
  51.  
  52. var secs = prompt('Check every how many seconds?', localStorage['wfi_secs'] || 30)*1;
  53. if(!secs) return;
  54.  
  55. localStorage['wfi_wfw'] = search;
  56. localStorage['wfi_secs'] = secs;
  57.  
  58. var div = document.createElement('div');
  59. div.innerHTML = 'Waiting for '+(search ? '"'+search+'"' : 'anything')+' every '+secs+' seconds... Last checked at <span id="wfi_last"/>';
  60. div.style.position = 'fixed';
  61. div.style.top = '0';
  62. div.style.left = '0';
  63. div.style.backgroundColor = '#ffc';
  64. document.body.appendChild(div);
  65. var last = document.getElementById('wfi_last');
  66.  
  67. var xhr = new XMLHttpRequest();
  68. function load(){
  69. document.body.removeChild(div);
  70. if(check(document)){
  71. alert((search ? '"'+search+'"' : 'somehing')+' found in '+document.title);
  72. }
  73. else{
  74. document.body.appendChild(div);
  75. xhr.open('GET', document.location.href, true);
  76. xhr.send();
  77. last.innerHTML = new Date();
  78. }
  79. }
  80.  
  81. var doc = document.implementation.createHTMLDocument('');
  82. xhr.onreadystatechange = function(){
  83. if (xhr.readyState == 4 && xhr.status == 200){
  84. doc.documentElement.innerHTML = xhr.responseText;
  85. if(check(doc)){
  86. alert((search ? '"'+search+'"' : 'somehing')+' found in '+document.title);
  87. document.location.reload();
  88. }
  89. else setTimeout(load, secs*1000);
  90. }
  91. };
  92.  
  93. load();
  94. });