2

According to this answer and this one I believe I can change the code inside a script src, am I right?

The page has the following head:

<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <link type="text/css" rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic,600,600italic,700,700italic,800,800italic&subset=latin,latin-ext">
    <script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js">
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
    <script src="/common/script/matomy/ads/basic.js" type="text/javascript">
    <script src="/common/script/matomy/ads/offer_page.js" type="text/javascript">
    <script src="/common/script/matomy/util/script2.js" type="text/javascript">
    <link type="text/css" rel="stylesheet" href="/common/css/landing/style18.css">
    <script src="/common/script/matomy/offer_status/script.js" type="text/javascript">
  </head>

I want to intercept the last one:

<script src="/common/script/matomy/offer_status/script.js" type="text/javascript">

which has the following code:

script.js

And remove this:

if (json == "0")  {
    setTimeout('OfferStatusChecker.checkOfferCompletion()', 30 * 1000);
    return;
 }

here is my code, the commented lines represents failed tries:

// @include     http://stats.matomy.com/click/?id=*
// @require     https://gist.github.com/raw/2620135/checkForBadJavascripts.js
// @run-at      document-start
// @grant       unsafeWindow
// ==/UserScript==


function replaceSrcScript (scriptNode) {

    //var scriptSrc   = scriptNode.textContent;
    //var scriptSrc = scriptNode.setAttribute("src", theCode() );
    /*
    scriptSrc = scriptSrc.replace (
        /if\s\(json\s==\s"0"\)\s+{\s+setTimeout\('OfferStatusChecker\.checkOfferCompletion\(\)',\s30\s\*\s1000\);\s+return;\s+}/,
        ""
    );*/

    //addJS_Node (null, null, scriptSrc);
    //addJS_Node (scriptSrc);
}
    /*
    checkForBadJavascripts ( [
        [   true, /script\.js/, replaceSrcScript   ]
    ] );
    */

    checkForBadJavascripts ( [
    [   true,
        /script\.js/,
        function () {
            addJS_Node (theCode.toString() );
        }
    ]
] );

function theCode() {
//inside here the script.js code modified, without the lines I want remove
}

Note that the function goes together, which results on a firebug ReferenceError. The same error happens when the script is replaced blank or removed.

code replaced


This script inside the body, calls the content of the src script:

<script>
  $(document).ready(function(){
    $(window).resize(function() {
      var theFrame = $("#offer");
      var topHeight = $(".offer_info").height();
      var newHeight = $(document.body).height() - topHeight;
      theFrame.height(newHeight);
    });

    OfferStatusChecker.checkOfferStatusURL = 'http://stats.matomy.com/offerstatus/?id=30a54408-64fb-42d7-9279-70afb747e364';
    OfferStatusChecker.onOfferCompleted = onOfferCompleted;
    OfferStatusChecker.start();
    setTimeout('initUI()', 1 * 1000);
 });

function initUI() {
  $(".inst_bar").slideDown('slow');
}

function onOfferCompleted() {
 $(".inst_bar").slideUp();
 $('.top_bar').slideUp('slow', function() {
   $(".status").html('Offer completed!');
   $(".right_side").css("background-image", "url('/common/css/landing/images/completed.gif')");
   $(".med_side").hide();
   $(".left_side").hide();
   $('.top_bar').slideDown();
 });
}
</script>

Neither change src path to file://...script.js , which I read one time, it was insecure to use, solved it.

Since, checkForBadJavascripts belongs to Brock Adams, I hope he can have a look on this question.


- - Update - -


I managed to replace the src, for an inline script, which removed the ReferenceErrors and read the code. But is this the right approach? I mean, would it work on any other case I have a src script, not just for this target page?

function replaceSrcScript () {

    var removeFunction = theCode.toString();
    removeFunction     = removeFunction.replace(/\s?function\stheCode\(\)\s?\{\s+/ , "");
    removeFunction     = removeFunction.slice( 0 , removeFunction.lastIndexOf("}") - 1 );

    addJS_Node (removeFunction);
}


checkForBadJavascripts ( [
    [   true, /script\.js/, replaceSrcScript   ]
] );


function theCode() {
//inside here the script.js code modified, without the lines I want remove
}

The objective of my question is to know the right solution to modify src scripts, not just make it work on the target page.

Community
  • 1
  • 1
Commentator
  • 640
  • 1
  • 6
  • 22
  • When changing the *contents* of a script from `src`, you have to block it, then AJAX it, then change contents of AJAX result then insert modified script. Can get tricky if there are dependencies. (Last I checked but that was several revs ago. Not gonna revisit this question until Wednesday at the earliest.) Use local copy rather than AJAX if script doesn't change often. All of this is Firefox-only. – Brock Adams Sep 09 '14 at 08:12
  • Thanks for your reply, Brock Adams. It looks like more complex than I thought – Commentator Sep 09 '14 at 17:46

0 Answers0