如何重写Javascript代码引用的URL? [英] How to rewrite URLs referenced by Javascript code?

查看:22
本文介绍了如何重写Javascript代码引用的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在技术上颇具挑战性的将HTML注入其他人的网站的后续措施?问题.>

回顾一下:我想向客户演示我的技术,而无需实际修改其实时网站(例如,在不修改实时网站的情况下演示Stackoverflow财务奖励的想法).本质上,我正在尝试创建 Greasemonkey 的服务器端版本.

我已经实现了如下镜像:

  1. 请求进入 http://myserver.com/forward?uri= [remote]
  2. 我的服务器打开一个与 [remote] 的连接,拉出数据并将其正文/标头返回到来自#1的请求.

我选择此语法是因为我需要处理来自多个域的请求(这意味着,如果 stackoverflow.com 链接到 meta.stackoverflow.com ,我需要处理两个域来自同一转发服务器).

我设法重写了HTML和CSS文件中的链接,因此它们是相对于我的服务器的.最后的障碍是重写Javascript文件引用的URL.

以编程方式重写别人的Javascript代码引用的URL的最佳方法是什么?在技​​术上这是否可行?


讨论

我将举例说明我面临的技术障碍.以 http://www.honda.com/为例.他们在页面上嵌入了Flash元素,但不是直接嵌入< object> ,而是使用Javascript来 document.write() < object>标记,其中包含网址.

第一次尝试

  1. 使用 https://stackoverflow.com/a/14570614/14731 监听DOM更改事件.与其尝试用Javascript代码重写URL,不如等待它修改DOM并即时重写URL.
  2. 使用 https://stackoverflow.com/a/629782/14731 拦截所有XmlHttpRequest请求

理想情况下,我们希望在DOM更改呈现之前对其进行拦截,因此浏览器在我们有机会重写它们之前不请求URL.

相关资源:

第二次尝试

服务器端解决方案将不起作用.即使我可以重写所有DOM URL,也已经看到了一个示例,其中嵌入式Flash应用程序引用了存储在Javascript变量中的URL.由于Flash应用程序是不透明的,因此无法通过编程方式来检测这些变量是否代表URL.

接下来,我打算尝试一种客户端解决方案.我将在一个框架中加载原始网站,并在第二个(隐藏)框架中使用Javascript处理其内容.我希望能够注入新的DOM元素(以演示我的产品),而不必重写现有元素.

解决方案

回答我自己的问题.

经过大量研究,我发现此技术最有效: https://stackoverflow.com/a/23231268/14731

换句话说,似乎没有通用的重写链接算法.手动修补它们并没有您期望的那么多.

This is a follow-up to my popular and technically challenging HTML injection into someone else's website? question.

To recap: I'd like to demo my technology to customers without actually modifying their live website (e.g. demoing the idea of Stackoverflow financial bounties, without modifying the live site). Essentially, I'm trying to create a server-side version of Greasemonkey.

I've implemented the mirror as follows:

  1. A request comes into http://myserver.com/forward?uri=[remote]
  2. My server opens a connect to [remote], pulls down the data and returns its body/headers to the request from #1.

I chose this syntax because I needed to handle requests from multiple domains (meaning, if stackoverflow.com links to meta.stackoverflow.com I need to handle both domains from the same forwarding server).

I have managed to rewrite links in the HTML and CSS files so they are relative to my server. The final hurdle is rewriting URLs referenced by Javascript files.

What is the best way to programmatically rewrite URLs referenced by someone else's Javascript code? Is this even technically doable?


Discussion

I'll give you an example of the technical hurdle I am facing. Take http://www.honda.com/ for example. They embed a Flash element on the page, but instead of embedding <object> directly, they use Javascript to document.write() the <object> tag containing the URL.

First attempt

  1. Use https://stackoverflow.com/a/14570614/14731 to listen for DOM change events. Instead of trying to rewrite URLs in the Javascript code, wait for it to modify the DOM and rewrite the URLs on the fly.
  2. Intercept all XmlHttpRequest requests using https://stackoverflow.com/a/629782/14731

Ideally we want intercept DOM changes before they render, so the browser does not request URLs before we have a chance to rewrite them.

Related resources:

Second attempt

A server-side solution will not work. Even if I can rewrite all DOM URLs, I've seen an example where an embedded Flash application references URLs stored in Javascript variables. There is no programmatic way to detect that these variables represent URLs because the Flash application is opaque.

Next, I plan on trying a client-side solution. I will load the original website in one frame, and manipulate its contents using Javascript in a second (hidden) frame. I hope to be able to inject new DOM elements (to demo my product) without having to rewrite the existing elements.

解决方案

Answering my own question.

After much research, I find this technique works best: https://stackoverflow.com/a/23231268/14731

In other words, there doesn't seem to be a general algorithm to rewrite links. Patching them by hand isn't as much work as you'd expect.

这篇关于如何重写Javascript代码引用的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆