Guest User

DAZ Shop (asof 20170530) - fix broken GA code (no recursion)

a guest
May 30th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        DAZ-GA-FIX
  3. // @namespace   daz3d.com*
  4. // @description Replaces broken chooseVariation() script with one that catches the error
  5. // @include     http://www.daz3d.com/*
  6. // @include     http://daz3d.com/*
  7. // @include     https://www.daz3d.com/*
  8. // @include     https://daz3d.com/*
  9. // @version     2
  10. // @grant       none
  11. // @author      mork
  12. // @license     no restrictions besides NOMIL+NOGOV
  13. // @run-at document-start
  14. // ==/UserScript==
  15. //
  16.  
  17.  
  18. /*
  19. v2 - 20170530 - fixed recursion - script suddenly started to replace itself after first replace.
  20.  */
  21.  
  22. // credits: http://stackoverflow.com/questions/3972038/stop-execution-of-javascript-function-client-side-or-tweak-it
  23. checkForBadJavascripts ( [
  24.     [false, { 'match' : /chooseVariation/, 'ignore' : /betterFunction/ } , function () {addJS_Node (betterFunction);} ]
  25. ] );
  26.  
  27.  
  28. function checkForBadJavascripts (controlArray) {
  29.     /*--- Note that this is a self-initializing function.  The controlArray
  30.         parameter is only active for the FIRST call.  After that, it is an
  31.         event listener.
  32.  
  33.         The control array row is  defines like so:
  34.         [bSearchSrcAttr, identifyingRegex, callbackFunction]
  35.         Where:
  36.             bSearchSrcAttr      True to search the SRC attribute of a script tag
  37.                                 false to search the TEXT content of a script tag.
  38.             identifyingRegex    A valid regular expression that should be unique
  39.                                 to that particular script tag.
  40.             callbackFunction    An optional function to execute when the script is
  41.                                 found.  Use null if not needed.
  42.     */
  43.     if ( ! controlArray.length) return null;
  44.  
  45.     checkForBadJavascripts      = function (zEvent) {
  46.  
  47.         for (var J = controlArray.length - 1;  J >= 0;  --J) {
  48.             var bSearchSrcAttr      = controlArray[J][0];
  49.             var identifyingRegex    = controlArray[J][1]['match'];
  50.             var ignoringRegex       = controlArray[J][1]['ignore'];
  51.  
  52.             if (bSearchSrcAttr) {
  53.                 if (identifyingRegex.test (zEvent.target.src) ) {
  54.                     stopBadJavascript (J);
  55.                     return false;
  56.                 }
  57.             }
  58.             else {
  59.                 if (identifyingRegex.test (zEvent.target.textContent) && !ignoringRegex.test(zEvent.target.textContent) ) {
  60.                     stopBadJavascript (J);
  61.                     return false;
  62.                 }
  63.             }
  64.         }
  65.  
  66.         function stopBadJavascript (controlIndex) {
  67.             zEvent.stopPropagation ();
  68.             zEvent.preventDefault ();
  69.  
  70.             var callbackFunction    = controlArray[J][2];
  71.             if (typeof callbackFunction == "function")
  72.                 callbackFunction ();
  73.  
  74.             //--- Remove the node just to clear clutter from Firebug inspection.
  75.             zEvent.target.parentNode.removeChild (zEvent.target);
  76.  
  77.             //--- Script is intercepted, remove it from the list.
  78.             controlArray.splice (J, 1);
  79.             if ( ! controlArray.length) {
  80.                 //--- All done, remove the listener.
  81.                 window.removeEventListener (
  82.                     'beforescriptexecute', checkForBadJavascripts, true
  83.                 );
  84.             }
  85.         }
  86.     }
  87.  
  88.     /*--- Use the "beforescriptexecute" event to monitor scipts as they are loaded.
  89.         See https://developer.mozilla.org/en/DOM/element.onbeforescriptexecute
  90.         Note that it does not work on acripts that are dynamically created.
  91.     */
  92.     window.addEventListener ('beforescriptexecute', checkForBadJavascripts, true);
  93.  
  94.     return checkForBadJavascripts;
  95. }
  96.  
  97.  
  98. function betterFunction()  {
  99.   var doorNumber = 0;
  100.   try {
  101.     doorNumber = cxApi.chooseVariation();
  102.     if (typeof doorNumber != 'number')  {
  103.       //console.log("doorNumber is not a number. Set to 0");
  104.       doorNumber = 0;
  105.     }
  106.   } catch (e) {}
  107.  
  108.   $(document).ready(function() {
  109.     var bodyElem = document.getElementsByTagName('body')[0];
  110.     bodyElem.className = bodyElem.className + " door_number_"+doorNumber;
  111.   });
  112. }
  113.  
  114.  
  115.  
  116. function addJS_Node (text, s_URL, funcToRun) {
  117.     var D                                   = document;
  118.     var scriptNode                          = D.createElement ('script');
  119.     scriptNode.type                         = "text/javascript";
  120.     if (text)       scriptNode.textContent  = text;
  121.     if (s_URL)      scriptNode.src          = s_URL;
  122.     if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';
  123.  
  124.     var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
  125.     //--- Don't error check here. if DOM not available, should throw error.
  126.     targ.appendChild (scriptNode);
  127. }
Advertisement
Add Comment
Please, Sign In to add comment