是否可以加载本地版本的 JavaScript 文件而不是服务器版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5516359/
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
Is it possible to load in a local version of a JavaScript file instead of the server version?
提问by Andir
Just had a quick question to throw out and see if there was a solution for this...
只是有一个快速的问题要抛出,看看是否有解决方案...
Let's pretend I have no access to the server. I load up a webpage and find out that they have a Javascript file loading from a subfolder (let's say /scripts/js/some.js)
假设我无法访问服务器。我加载了一个网页,发现他们有一个从子文件夹加载的 Javascript 文件(比如 /scripts/js/some.js)
Now, I want to make changes to this file locally and test it against the whole site without downloading the entire site to a local folder.
现在,我想在本地更改此文件并针对整个站点对其进行测试,而无需将整个站点下载到本地文件夹。
Does anyone know of a way I can override the loading of that remote js file in favor of a local/edited copy of it?
有谁知道我可以覆盖该远程 js 文件的加载以支持它的本地/编辑副本的方法吗?
采纳答案by Andir
I actually found a solution for this. Posting details for anyone that comes here looking for it.
我实际上为此找到了解决方案。发布任何来这里寻找它的人的详细信息。
Privoxy (www.privoxy.org/) [Free] Allows this for the most part through a redirect. Though Firefox may block the redirect depending on where you put it. This means you most likely will not be able to save the file locally and reference it via file://etc/
Privoxy (www.privoxy.org/) [免费] 在大多数情况下允许通过重定向来实现。尽管 Firefox 可能会根据您放置的位置阻止重定向。这意味着您很可能无法在本地保存文件并通过 file://etc/ 引用它
( I wish I had a way to tell you how to staticallyfiddle with JavaScript on web pages you have limited access to... but I have not found it. If an answer comes along I will accept it over this.)
(我希望我有一种方法可以告诉您如何在您访问权限有限的网页上静态地摆弄 JavaScript ......但我还没有找到它。 如果有答案,我会接受它。)
Of course, you have to set up Privoxy, and use it as a local proxy server. It's pretty simple if you only use it temporarily: Just point your browser to proxy 127.0.0.1 on port 8118 with it running.
当然,您必须设置 Privoxy,并将其用作本地代理服务器。如果您只是暂时使用它,这非常简单:只需将您的浏览器指向端口 8118 上的代理 127.0.0.1 并运行它。
You have to add a redirect "default action" (Options > Edit Default Actions) to redirect the browser to use your new copy:
您必须添加重定向“默认操作”(选项 > 编辑默认操作)以重定向浏览器以使用您的新副本:
{ +redirect{/newLocation/some.js} }
/scripts/js/some.js
回答by Mike Park
Try using noscriptor adblock to block the server side script from loading. Then use greasemonkeyto load your own script.
尝试使用noscript或 adblock 来阻止加载服务器端脚本。然后使用greasemonkey加载自己的脚本。
回答by Kyle Ridolfo
If you want a way to use a local file instead of a remote file (in any web browser), I highly recommend Charles Web Proxy. http://www.charlesproxy.com/
如果您想要一种使用本地文件而不是远程文件的方法(在任何 Web 浏览器中),我强烈推荐 Charles Web Proxy。http://www.charlesproxy.com/
In Charles, go to the Tools menu and select Map Local. Add a new mapping by entering the address of the file on the web you would like loaded from your disk.
在 Charles 中,转到“工具”菜单并选择“本地地图”。通过在网络上输入您希望从磁盘加载的文件地址来添加新映射。
This technique will for all sorts of files (JavaScript, CSS, SWF). Of course you have the option to temporarily disable this feature, and it will only work while Charles is running. Very handy.
这种技术适用于各种文件(JavaScript、CSS、SWF)。当然,您可以选择暂时禁用此功能,它仅在 Charles 运行时有效。非常便利。
回答by Goro
While your solution with proxy is somewhat more permanent, I found that with Fiddler you can do it with almost no configuration:
虽然您使用代理的解决方案更持久一些,但我发现使用 Fiddler 您几乎可以在没有配置的情况下完成它:
How to replace Javascript of production website with local Javascript?
回答by Jim Blackler
In a browser that supports FileReader such as Chrome, yes, in combination with 'eval' to execute arbitrary JS. In your HTML add a button for the user to press:
在Chrome等支持FileReader的浏览器中,可以,结合'eval'执行任意JS。在您的 HTML 中添加一个按钮供用户按下:
<form>
<input type="file" name="file"
onchange="loadJS(event.target.files);">
</form>
In your scripts add:
在您的脚本中添加:
function load() {
var reader = new FileReader();
reader.onload = function(evt) {
eval(evt.target.result);
};
reader.readAsText(files[0]);
}