如何修改或替换脚本src代码

时间:2014-09-09 06:56:24

标签: firefox replace greasemonkey

根据this answerthis one 我相信我可以更改脚本src中的代码,我是对的吗?

该页面包含以下内容:

<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>

我想拦截最后一个:

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

,其代码如下:

script.js

并删除此内容:

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

这是我的代码,注释行代表失败的尝试:

// @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
}

请注意,该函数结合在一起,导致firebug引用。将脚本替换为空白或删除时会发生同样的错误。

code replaced


正文中的这个脚本调用src脚本的内容:

<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>

没有将src路径更改为file://...script.js,我读了一次,使用不安全,解决了它。

因为,checkForBadJavascripts属于布洛克亚当斯,我希望他能看看这个问题。


- - 更新 - -


我设法替换了src,用于内联脚本,它删除了ReferenceErrors并读取了代码。但这是正确的方法吗?我的意思是,它是否适用于任何其他情况我有一个src脚本,而不仅仅是这个目标页面?

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
}

我的问题的目标是了解修改src脚本的正确解决方案,而不仅仅是让它在目标页面上运行。

0 个答案:

没有答案