Fossil SCM
Converted src/href.js to jQuery as a demonstration: the code's nearly half the size, and it's clearer as a result.
Commit
59c7ca0509e49872a09f3a89bb6cc55a72ca483c24e4bc720205bac3af6f3f7f
Parent
e806e5c4df71f2e…
1 file changed
+15
-26
+15
-26
| --- src/href.js | ||
| +++ src/href.js | ||
| @@ -16,31 +16,20 @@ | ||
| 16 | 16 | ** milliseconds delay prior to populating href= and action=. If the |
| 17 | 17 | ** mouseover boolean is true, then the timer does not start until a |
| 18 | 18 | ** mouse motion event occurs over top of the document. |
| 19 | 19 | */ |
| 20 | 20 | function setAllHrefs(){ |
| 21 | - var anchors = document.getElementsByTagName("a"); | |
| 22 | - for(var i=0; i<anchors.length; i++){ | |
| 23 | - var j = anchors[i]; | |
| 24 | - if(j.hasAttribute("data-href")) j.href=j.getAttribute("data-href"); | |
| 25 | - } | |
| 26 | - var forms = document.getElementsByTagName("form"); | |
| 27 | - for(var i=0; i<forms.length; i++){ | |
| 28 | - var j = forms[i]; | |
| 29 | - if(j.hasAttribute("data-action")) j.action=j.getAttribute("data-action"); | |
| 30 | - } | |
| 31 | -} | |
| 32 | -function antiRobotDefense(){ | |
| 33 | - var x = document.getElementById("href-data"); | |
| 34 | - var jx = x.textContent || x.innerText; | |
| 35 | - var g = JSON.parse(jx); | |
| 36 | - var isOperaMini = | |
| 37 | - Object.prototype.toString.call(window.operamini)==="[object OperaMini]"; | |
| 38 | - if(g.mouseover && !isOperaMini){ | |
| 39 | - document.getElementByTagName("body")[0].onmousemove=function(){ | |
| 40 | - setTimeout(setAllHrefs, g.delay); | |
| 41 | - } | |
| 42 | - }else{ | |
| 43 | - setTimeout(setAllHrefs, g.delay); | |
| 44 | - } | |
| 45 | -} | |
| 46 | -antiRobotDefense() | |
| 21 | + $('a[data-href]').attr('href', function() { | |
| 22 | + return this.attr("data-href"); | |
| 23 | + }); | |
| 24 | + $('form[data-action]').attr('action'), function() { | |
| 25 | + return this.attr("data-action"); | |
| 26 | + }); | |
| 27 | +} | |
| 28 | +(function antiRobotDefense(){ | |
| 29 | + var g = JSON.parse($('#href-data').text()); | |
| 30 | + var hasMouseMove = 'onmousemove' in createElement('body'); | |
| 31 | + var initEvent = g.mouseover && hasMouseMove ? 'mousemove' : 'load'; | |
| 32 | + $('body').on(initEvent, function() { | |
| 33 | + setTimeout(setAllHrefs, g.delay); | |
| 34 | + }); | |
| 35 | +})(); | |
| 47 | 36 |
| --- src/href.js | |
| +++ src/href.js | |
| @@ -16,31 +16,20 @@ | |
| 16 | ** milliseconds delay prior to populating href= and action=. If the |
| 17 | ** mouseover boolean is true, then the timer does not start until a |
| 18 | ** mouse motion event occurs over top of the document. |
| 19 | */ |
| 20 | function setAllHrefs(){ |
| 21 | var anchors = document.getElementsByTagName("a"); |
| 22 | for(var i=0; i<anchors.length; i++){ |
| 23 | var j = anchors[i]; |
| 24 | if(j.hasAttribute("data-href")) j.href=j.getAttribute("data-href"); |
| 25 | } |
| 26 | var forms = document.getElementsByTagName("form"); |
| 27 | for(var i=0; i<forms.length; i++){ |
| 28 | var j = forms[i]; |
| 29 | if(j.hasAttribute("data-action")) j.action=j.getAttribute("data-action"); |
| 30 | } |
| 31 | } |
| 32 | function antiRobotDefense(){ |
| 33 | var x = document.getElementById("href-data"); |
| 34 | var jx = x.textContent || x.innerText; |
| 35 | var g = JSON.parse(jx); |
| 36 | var isOperaMini = |
| 37 | Object.prototype.toString.call(window.operamini)==="[object OperaMini]"; |
| 38 | if(g.mouseover && !isOperaMini){ |
| 39 | document.getElementByTagName("body")[0].onmousemove=function(){ |
| 40 | setTimeout(setAllHrefs, g.delay); |
| 41 | } |
| 42 | }else{ |
| 43 | setTimeout(setAllHrefs, g.delay); |
| 44 | } |
| 45 | } |
| 46 | antiRobotDefense() |
| 47 |
| --- src/href.js | |
| +++ src/href.js | |
| @@ -16,31 +16,20 @@ | |
| 16 | ** milliseconds delay prior to populating href= and action=. If the |
| 17 | ** mouseover boolean is true, then the timer does not start until a |
| 18 | ** mouse motion event occurs over top of the document. |
| 19 | */ |
| 20 | function setAllHrefs(){ |
| 21 | $('a[data-href]').attr('href', function() { |
| 22 | return this.attr("data-href"); |
| 23 | }); |
| 24 | $('form[data-action]').attr('action'), function() { |
| 25 | return this.attr("data-action"); |
| 26 | }); |
| 27 | } |
| 28 | (function antiRobotDefense(){ |
| 29 | var g = JSON.parse($('#href-data').text()); |
| 30 | var hasMouseMove = 'onmousemove' in createElement('body'); |
| 31 | var initEvent = g.mouseover && hasMouseMove ? 'mousemove' : 'load'; |
| 32 | $('body').on(initEvent, function() { |
| 33 | setTimeout(setAllHrefs, g.delay); |
| 34 | }); |
| 35 | })(); |
| 36 |