Jac*_*k M 9 html javascript code-injection
我想将几个本地.js文件注入网页.我只是指客户端,就像在我的浏览器中一样,我不需要任何其他人访问该页面就可以看到它.我只需要一个.js文件,然后制作它就好像这个文件一直都是通过<script>
标签包含在页面的html中.
如果页面加载后本地文件中的内容可用,则可以.
如果我必须在计算机上"手动"使用控制台或其他东西,这是没关系的.
我已经尝试了两天,我尝试过Greasemonkey,我尝试使用JavaScript控制台手动加载文件.让我感到惊讶的是,没有(显然)已有的方法可以做到这一点,这似乎是一件非常简单的事情.不过,我认为简单与普通不同.
如果它有帮助,我想这样做的原因是在基于JS的聊天客户端上运行聊天机器人.一些机器人的代码被混合到预先存在的聊天代码中 - 为此,我让Fiddler拦截对.../chat.js的请求并将其替换为本地文件.但我有两个.js文件,它们与页面本身的任何内容"独立".页面请求的任何.js文件都没有我可以替换它们,所以我不能使用Fiddler.
由于您已经使用了 fiddler 脚本,因此您可以在函数中执行类似的OnBeforeResponse(oSession: Session)
操作
if ( oSession.oResponse.headers.ExistsAndContains("Content-Type", "html") &&
oSession.hostname.Contains("MY.TargetSite.com") ) {
oSession.oResponse.headers.Add("DEBUG1_WE_EDITED_THIS", "HERE");
// Remove any compression or chunking
oSession.utilDecodeResponse();
var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
// Find the end of the HEAD script, so you can inject script block there.
var oRegEx = oRegEx = /(<\/head>)/gi
// replace the head-close tag with new-script + head-close
oBody = oBody.replace(oRegEx, "<script type='text/javascript'>console.log('We injected it');</script></head>");
// Set the response body to the changed body string
oSession.utilSetResponseBody(oBody);
}
Run Code Online (Sandbox Code Playgroud)
www.html5rocks.com 的工作示例:
if ( oSession.oResponse.headers.ExistsAndContains("Content-Type", "html") &&
oSession.hostname.Contains("html5rocks") ) { //goto html5rocks.com
oSession.oResponse.headers.Add("DEBUG1_WE_EDITED_THIS", "HERE");
oSession.utilDecodeResponse();
var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
var oRegEx = oRegEx = /(<\/head>)/gi
oBody = oBody.replace(oRegEx, "<script type='text/javascript'>alert('We injected it')</script></head>");
oSession.utilSetResponseBody(oBody);
}
Run Code Online (Sandbox Code Playgroud)
请注意,您必须在 fiddler 中关闭流式传输:http://www.fiddler2.com/fiddler/help/streaming.asp,我假设您需要解码 HTTPS:http://www.fiddler2.com/fiddler/帮助/httpsdecryption.asp
我越来越少地使用 fiddler 脚本,转而使用 fiddler .Net 扩展 - http://fiddler2.com/fiddler/dev/IFiddlerExtension.asp