Chrome 扩展程序,在浏览器显示响应代码之前替换 HTML
我想知道是否有某种方法可以做类似的事情: 如果我在特定站点上,我希望直接从我的计算机加载一些 javascript 文件(fe file:///c:/test.js),而不是从服务器加载。
为此,我在想是否有可能制作一个扩展程序,可以更改浏览器在显示之前得到的响应中的 HTML 代码。所以整个过程应该是这样的:
- 发出请求,
- 浏览器从服务器获取响应
- #响应已更改# - 这是扩展进入
- 浏览器时解析更改的响应并使用新响应显示页面的部分。
无论如何,它甚至不必是 Chrome 扩展。它应该只完成上述工作。它可以阻止原始文件并提供另一个文件(DNS/代理?)或过滤我的计算机中的整个 HTTP 流量并将特定代码替换为另一个匹配的响应。
i wonder if there is some way to do something like that:
If im on a specific site i want that some of javascript files to be loaded directly from my computer (f.e. file:///c:/test.js), not from the server.
For that i was thinking if there is a possibility to make an extension which could change HTML code in a response which browser gets right before displaying it. So whole process should look like that:
- request is made
- browser gets response from server
- #response is changed# - this is the part when extension comes in
- browser parse changed response and display page with that new response.
It doesnt even have to be a Chrome extension anyway. It should just do the job described above. It can block original file and serve another one (DNS/proxy?) or filter whole HTTP traffic in my computer and replace specific code to another one of matched response.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 WebRequest API 来实现此目的。例如,您可以添加一个
onBeforeRequest
监听器并重定向一些请求:这将显示一个带有文本“some text”的
元素,而不是 Google 主页。请注意,您只能重定向到 Web 服务器本身允许重定向到的 URL。这意味着无法重定向到
file:///
URL,并且您只能重定向到扩展程序内的文件,前提是这些文件是 网络可访问。然而,data:
和http:
URL 工作正常。You can use the WebRequest API to achieve that. For example, you can add a
onBeforeRequest
listener and redirect some requests:This will display a
<div>
element with the text "some text" instead of the Google homepage. Note that you can only redirect to URLs that the web server itself is allowed to redirect to. This means that redirecting tofile:///
URLs is not possible, and you can only redirect to files inside your extension if these are web accessible.data:
andhttp:
URLs work fine however.在 Windows 中,您可以使用 Proxomitron (proxomitron.info),它是一个本地代理,可以拦截正在加载到浏览器中的任何页面或文件,并在由渲染器渲染之前使用正则表达式(无 DOM 解析)对其进行更改。浏览器。
In Windows you can use the Proxomitron (proxomitron.info) which is a local proxy that can intercept any page or file being loading into your browser and change it using regular expressions (no DOM parsing) however you want, before it is rendered by the browser.