Opera User-JS:如何获取原始服务器响应?

发布于 2024-10-07 06:00:31 字数 1143 浏览 3 评论 0原文

我正在为 Opera 编写一些用户 JS。它对没有扩展名的请求做出反应,例如 /stuff/code/MyFile,或者具有与 JavaScript 无关的扩展名,例如 /stuff/code/load.do.响应的内容类型设置为 text/html,即使它返回纯 JavaScript 源 (text/javascript)。由于我无权访问服务器代码,我只能忍受这一点。

现在的问题是我想用行号等格式化源代码并在 Opera 中显示它。因此,我编写了一些用户 JS 来对 AfterEvent.DOMContentLoaded 做出反应(也尝试了 AfterEvent.load,同样的事情)。它读取e.event.target.body.innerHTML来访问正文,即JavaScript代码。

如果源代码不包含 HTML 标签或比较运算符(<、>),那么仅此一项就可以很好地工作。既然如此,我永远不会得到我想要的输出。 Opera 似乎有一些内部逻辑可以将 text/html 响应转换为其自己的表示格式。这包括例如删除 HTML 标签后的 CRLF 或两个“匹配”之间的代码。和> (比较运算符!)被压缩成一行,在每个单词后面应用 =""

这就是问题所在。

如果我在没有用户 JS 的情况下请求相同的 URL,然后查看“页面”的源代码,我会看到与服务器发送的内容相同的干净 JavaScript 代码。这就是我想要访问的内容。

如果我使用 innerText 而不是 innerHTML,Opera 也会删除 HTML 标签,使文件与原始文件不同。

我还尝试查看 outerHTMLouterTexttextContent,但它们都有相同的问题。

我知道 Opera 在这里并没有做错什么。服务器说它是一个 text/html,而 Opera 只是执行它通常对 text/html 类型的响应所做的事情。

因此,我的问题是:有没有办法通过用户 JS 获得未受影响的响应?

I'm writing some user-JS for Opera. It reacts on a request that doesn't have an extension, e.g. /stuff/code/MyFile, or has one not related to JavaScript, e.g. /stuff/code/load.do. The content-type of the response is set to text/html, even though it returns pure JavaScript source (text/javascript). As I don't have access to the server code I simply have to live with this.

The problem now is that I want to format the source with line numbers and such and display it inside Opera. Therefore, I wrote some user-JS to react on AfterEvent.DOMContentLoaded (also tried AfterEvent.load, same thing). It reads e.event.target.body.innerHTML to gain access to the body, i.e. the JavaScript-code.

That alone would work nicely, if only the source wouldn't contain HTML-tags or comparison operators (<, >). Since it does, I never get the output I want. Opera seems to have some internal logic to convert the text/html-response into its own representation format. This includes that e.g. a CRLF after a HTML-tag is removed or code between two "matching" < and > (comparison operators!) are crunched together into one single line applying ="" after each word in there.

And that's where the problem is.

If I request the same URL without my user-JS and then look at the source of the "page" I see a clean JavaScript-code identical to what the server sent out. And this is what I want to get access to.

If I use innerText instead of innerHTML, Opera strips out the HTML-tags making the file different to the original, too.

I also tried to look at outerHTML, outerText and textContent, but they all have the same problems.

I know that Opera doesn't do anything wrong here. The server says it's a text/html and Opera simply does what it usually does with a text/html-kind of response.

Therefore, my question is: is there any way to get the untouched response with a user-JS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夏花。依旧 2024-10-14 06:00:31

没有任何方法可以从 JS 访问预解析的标记。唯一的方法是使用 XMLHttpRequest 自己请求内容。

There isn't any way to access the pre-parsed markup from JS. The only way to do that would be to use XMLHttpRequest to request the content yourself.

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