Advertisement
thetenfold

Untitled

Jul 14th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.addEventListener('load', function() {
  2.  
  3.     var challenges = document.getElementById('challenges'),
  4.         challenge, i, len;
  5.  
  6.     function change(e) {
  7.         if(e && e.target && e.target.tagName === 'LI') {
  8.  
  9.             // if this func is called from the listener, check if it's the element we want
  10.             e = e.target.querySelector('a.btn[data-action="play"][data-challenge-id]:not(atchd)');
  11.             if(e) {
  12.                 e.setAttribute('atchd', '1'); // this is so we know we grabbed it already
  13.                 e.textContent = 'Accept [atchd]';
  14.                 processElem(e); // do a callback
  15.             }
  16.         } else {
  17.  
  18.             // if this func isn't called from the listener, grab all current un-grabbed buttons
  19.             e = challenges.querySelectorAll('a.btn[data-action="play"][data-challenge-id]:not(atchd)');
  20.             if(e.length !== 0) {
  21.                 len = e.length;
  22.                 for (i = 0; i < len; i += 1) {
  23.                     challenge = e[i];
  24.                     challenge.setAttribute('atchd', '1'); // this is so we know we grabbed it already
  25.                     challenge.textContent = 'Accept [atchd]';
  26.                     processElem(e); // do a callback
  27.                 }
  28.             }
  29.         }
  30.     }
  31.    
  32.     function processElem(element) {
  33.         // do whatever we want with the anchor node element
  34.     }
  35.  
  36.     // it found the common ancestor
  37.     if(challenges) {
  38.         change(); // run it for the first time
  39.         challenges.addEventListener('DOMNodeInserted', change, false); // listen for changes
  40.     }
  41.  
  42. }, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement