Chrome 扩展程序,在浏览器显示响应代码之前替换 HTML

发布于 2024-11-14 12:02:25 字数 386 浏览 3 评论 0原文

我想知道是否有某种方法可以做类似的事情: 如果我在特定站点上,我希望直接从我的计算机加载一些 javascript 文件(fe file:///c:/test.js),而不是从服务器加载。

为此,我在想是否有可能制作一个扩展程序,可以更改浏览器在显示之前得到的响应中的 HTML 代码。所以整个过程应该是这样的:

  1. 发出请求,
  2. 浏览器从服务器获取响应
  3. #响应已更改# - 这是扩展进入
  4. 浏览器时解析更改的响应并使用新响应显示页面的部分。

无论如何,它甚至不必是 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:

  1. request is made
  2. browser gets response from server
  3. #response is changed# - this is the part when extension comes in
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

和影子一齐双人舞 2024-11-21 12:02:25

您可以使用 WebRequest API 来实现此目的。例如,您可以添加一个 onBeforeRequest 监听器并重定向一些请求:

chrome.webRequest.onBeforeRequest.addListener(function(details)
{
  var responseData = "<div>Some text</div>"
  return {redirectUrl: "data:text/html," + encodeURIComponent(responseData)};
}, {urls: ["https://www.google.com/"]}, ["blocking"]);

这将显示一个带有文本“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:

chrome.webRequest.onBeforeRequest.addListener(function(details)
{
  var responseData = "<div>Some text</div>"
  return {redirectUrl: "data:text/html," + encodeURIComponent(responseData)};
}, {urls: ["https://www.google.com/"]}, ["blocking"]);

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 to file:/// URLs is not possible, and you can only redirect to files inside your extension if these are web accessible. data: and http: URLs work fine however.

淡写薰衣草的香 2024-11-21 12:02:25

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文