Guest User

DAZ Shop (asof 20170315) - fix broken GA experimental code

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