无需 createElement 的动态 JavaScript 注入

发布于 2024-09-07 10:15:35 字数 263 浏览 0 评论 0原文

在 Google Gears API 中的“线程”上下文中运行代码时,您无权访问“文档”对象,因此 createElement 无法用于动态加载脚本。

有谁知道我在这种情况下如何“注入”代码?我能想到的唯一方法是使用 Web 服务和 JSON 对象,然后我会对其进行评估,但这会产生一系列问题和复杂性,并给我留下不好的印象。

问题是由于这个限制,我最终得到了大量的整体代码和各种重复的代码,没有什么比我更讨厌的了。

有什么想法吗?

When running code in the context of a "thread" in the Google Gears API you do not have access to the "document" object and therefore createElement cannot be used to dynamically load a script.

Does anyone have any ideas about how I might go about "injecting" code in such a scenario? The only method I can think of is using a webservice and a JSON object which I would then eval but this creates its own series of problems and complexities and leaves a bad taste in my mouth.

The problem is I am ending up with large monolithic bits of code and various duplication of code due to this limitation and there is nothing that I hate more.

Any ideas?

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

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

发布评论

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

评论(1

彩虹直至黑白 2024-09-14 10:15:35

无需使用 Web 服务,只需使用纯 XMLHttpRequest 来检索 javascript 文件,然后对其进行评估。

var xhr = new XMLHttpRequest();
xhr.open("foo.js", null, false); // since this is in a thread you can use the synchronous approach
xhr.send("");
eval(xhr.responseText);

No need to use a webservice, just use plain XMLHttpRequest to retrieve the javascript file, and then eval it.

var xhr = new XMLHttpRequest();
xhr.open("foo.js", null, false); // since this is in a thread you can use the synchronous approach
xhr.send("");
eval(xhr.responseText);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文