Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name DAZ-GA-FIX
- // @namespace daz3d.com*
- // @description Replaces broken chooseVariation() script with one that catches the error
- // @include http://www.daz3d.com/*
- // @include http://daz3d.com/*
- // @include https://www.daz3d.com/*
- // @include https://daz3d.com/*
- // @version 2
- // @grant none
- // @author mork
- // @license no restrictions besides NOMIL+NOGOV
- // @run-at document-start
- // ==/UserScript==
- //
- /*
- v2 - 20170530 - fixed recursion - script suddenly started to replace itself after first replace.
- */
- // credits: http://stackoverflow.com/questions/3972038/stop-execution-of-javascript-function-client-side-or-tweak-it
- checkForBadJavascripts ( [
- [false, { 'match' : /chooseVariation/, 'ignore' : /betterFunction/ } , function () {addJS_Node (betterFunction);} ]
- ] );
- function checkForBadJavascripts (controlArray) {
- /*--- Note that this is a self-initializing function. The controlArray
- parameter is only active for the FIRST call. After that, it is an
- event listener.
- The control array row is defines like so:
- [bSearchSrcAttr, identifyingRegex, callbackFunction]
- Where:
- bSearchSrcAttr True to search the SRC attribute of a script tag
- false to search the TEXT content of a script tag.
- identifyingRegex A valid regular expression that should be unique
- to that particular script tag.
- callbackFunction An optional function to execute when the script is
- found. Use null if not needed.
- */
- if ( ! controlArray.length) return null;
- checkForBadJavascripts = function (zEvent) {
- for (var J = controlArray.length - 1; J >= 0; --J) {
- var bSearchSrcAttr = controlArray[J][0];
- var identifyingRegex = controlArray[J][1]['match'];
- var ignoringRegex = controlArray[J][1]['ignore'];
- if (bSearchSrcAttr) {
- if (identifyingRegex.test (zEvent.target.src) ) {
- stopBadJavascript (J);
- return false;
- }
- }
- else {
- if (identifyingRegex.test (zEvent.target.textContent) && !ignoringRegex.test(zEvent.target.textContent) ) {
- stopBadJavascript (J);
- return false;
- }
- }
- }
- function stopBadJavascript (controlIndex) {
- zEvent.stopPropagation ();
- zEvent.preventDefault ();
- var callbackFunction = controlArray[J][2];
- if (typeof callbackFunction == "function")
- callbackFunction ();
- //--- Remove the node just to clear clutter from Firebug inspection.
- zEvent.target.parentNode.removeChild (zEvent.target);
- //--- Script is intercepted, remove it from the list.
- controlArray.splice (J, 1);
- if ( ! controlArray.length) {
- //--- All done, remove the listener.
- window.removeEventListener (
- 'beforescriptexecute', checkForBadJavascripts, true
- );
- }
- }
- }
- /*--- Use the "beforescriptexecute" event to monitor scipts as they are loaded.
- See https://developer.mozilla.org/en/DOM/element.onbeforescriptexecute
- Note that it does not work on acripts that are dynamically created.
- */
- window.addEventListener ('beforescriptexecute', checkForBadJavascripts, true);
- return checkForBadJavascripts;
- }
- function betterFunction() {
- var doorNumber = 0;
- try {
- doorNumber = cxApi.chooseVariation();
- if (typeof doorNumber != 'number') {
- //console.log("doorNumber is not a number. Set to 0");
- doorNumber = 0;
- }
- } catch (e) {}
- $(document).ready(function() {
- var bodyElem = document.getElementsByTagName('body')[0];
- bodyElem.className = bodyElem.className + " door_number_"+doorNumber;
- });
- }
- function addJS_Node (text, s_URL, funcToRun) {
- var D = document;
- var scriptNode = D.createElement ('script');
- scriptNode.type = "text/javascript";
- if (text) scriptNode.textContent = text;
- if (s_URL) scriptNode.src = s_URL;
- if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
- var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
- //--- Don't error check here. if DOM not available, should throw error.
- targ.appendChild (scriptNode);
- }
Advertisement
Add Comment
Please, Sign In to add comment