javascript 将本地 .js 文件注入网页?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10366084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inject local .js file into a webpage?
提问by Hyman M
I'd like to inject a couple of local .js files into a webpage. I just mean client side, as in within my browser, I don't need anybody else accessing the page to be able to see it. I just need to take a .js file, and then make it so it's as if that file had been included in the page's html via a <script>
tag all along.
我想将几个本地 .js 文件注入网页。我只是指客户端,就像在我的浏览器中一样,我不需要任何其他人访问该页面就能看到它。我只需要获取一个 .js 文件,然后制作它,就好像该文件一直通过<script>
标签包含在页面的 html 中一样。
It's okay if it takes a second after the page has loaded for the stuff in the local files to be available.
如果页面加载后需要一秒钟才能使本地文件中的内容可用,那也没关系。
It's okay if I have to be at the computer to do this "by hand" with a console or something.
如果我必须在计算机上使用控制台或其他东西“手动”执行此操作,那也没关系。
I've been trying to do this for two days, I've tried Greasemonkey, I've tried manually loading files using a JavaScript console. It amazes me that there isn't (apparently) an established way to do this, it seems like such a simple thing to want to do. I guess simple isn't the same thing as common, though.
我已经尝试这样做了两天,我试过 Greasemonkey,我试过使用 JavaScript 控制台手动加载文件。令我惊讶的是,没有(显然)一种既定的方法可以做到这一点,这似乎是一件很简单的事情。不过,我想简单与普通不同。
If it helps, the reason why I want to do this is to run a chatbot on a JS-based chat client. Some of the bot's code is mixed into the pre-existing chat code -- for that, I have Fiddler intercepting requests to .../chat.js and replacing it with a local file. But I have two .js files which are "independant" of anything on the page itself. There aren't any .js files requested by the page that I can substitute them for, so I can't use Fiddler.
如果有帮助,我想这样做的原因是在基于 JS 的聊天客户端上运行聊天机器人。一些 bot 的代码混合到预先存在的聊天代码中——为此,我让 Fiddler 拦截对 .../chat.js 的请求并将其替换为本地文件。但是我有两个 .js 文件,它们“独立”于页面本身的任何内容。没有任何页面请求的 .js 文件可以替代它们,所以我不能使用 Fiddler。
采纳答案by eSniff
Since your already using a fiddler script, you can do something like this in the OnBeforeResponse(oSession: Session)
function
由于您已经在使用提琴手脚本,因此您可以在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);
}
Working example for www.html5rocks.com :
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);
}
Note, you have to turn streaming off in fiddler : http://www.fiddler2.com/fiddler/help/streaming.aspand I assume you would need to decode HTTPS : http://www.fiddler2.com/fiddler/help/httpsdecryption.asp
请注意,您必须在 fiddler 中关闭流式传输:http: //www.fiddler2.com/fiddler/help/streaming.asp,我假设您需要解码 HTTPS:http: //www.fiddler2.com/fiddler/帮助/httpsdecryption.asp
I have been using fiddler script less and less, in favor of fiddler .Net Extensions - http://fiddler2.com/fiddler/dev/IFiddlerExtension.asp
我越来越少地使用提琴手脚本,支持提琴手 .Net 扩展 - http://fiddler2.com/fiddler/dev/IFiddlerExtension.asp
回答by Tyler C
回答by Dan Appleyard
How about just using jquery's jQuery.getScript() method?
只使用 jquery 的 jQuery.getScript() 方法怎么样?
回答by Bruce Lowe
save the normal html pages to the file system, add the js files manually by hand, and then use fiddler to intercept those calls so you get your version of the html file
将普通的html页面保存到文件系统中,手动添加js文件,然后使用fiddler拦截这些调用,这样你就得到了你的html文件版本