如何从外部影响页面中运行的 JavaScript
我希望能够通过在外部执行某些操作或设置某些内容,使网页中的 JavaScript 表现出不同的行为。因此,将进行逻辑检查(某物)是否执行功能一,否则执行功能二。我可以想到三种方法来做到这一点,但我不喜欢其中任何一种来选择它。至少不会,除非我能看看是否还有另一种明显的方法可以做到这一点,但目前我却不知何故。
向 URL 添加一个无害的查询字符串(例如?choose_functionity_one=true),我的逻辑可以简单地在页面 URL 中查找。我不喜欢这种情况的原因是我的代码在跨域 iframe 内运行,我什至无法访问页面的 URL(只能访问 iframe 的 URL)。是的,我可以将查询字符串传递到 iframe,但前提是我可以控制父页面,而我没有。
在页面的域中创建一个cookie,我的逻辑可以简单地在document.cookie中查找它。这可以通过小书签轻松完成,并且不会遇到跨域问题,因为我只需打开一个指向运行代码的域的窗口/选项卡,并在该上下文中运行小书签。这是我目前的领先选择。
向浏览器的用户代理字符串添加一些内容,并在我的逻辑中查找它。在 Firefox 上通过 about:config 非常简单,但在其他浏览器上则不太容易,在 Mac 上则非常困难。此外,在某些浏览器上,一旦设置了自定义值,您就无法在浏览器更新时自动更新 UA。您的 UA 版本信息会及时停留在您第一次自定义它时的位置。
任何人都可以想出另一种方式,通过电子邮件/即时消息/电话,我可以对某人说“这样做”,他们会看到页面的行为有所不同,以进行故障排除。对于没有这样做的普通人群来说,它运行完全正常。
I'd like to be able to make the JavaScript I've got in a web page behave differently by doing something or setting something externally. So there'll be logic checking if(something) execute functionality one, else execute functionality two. I can think of three ways of doing this, but I don't like any of them enough to choose it. At least not unless I can see if there's another blindingly obvious way of doing it that's somehow escaping me at the moment.
Add a harmless query string to the URL (e.g. ?choose_functionality_one=true) and my logic can simply look in the page URL. The reason I don't like this is the case where my code is running inside a cross-domain iframe and I can't even access the page's URL (only the iframe's URL). Yes I could pass the query string to the iframe, but only if I have control over the parent page, and I don't.
Create a cookie in the domain of the page, and my logic can simply look for it in document.cookie. This could be done with a bookmarklet easily enough, and wouldn't suffer from the cross-domain problem, because I simply open a window/tab to the domain where my code is running and run the bookmarklet in that context. This is my front-runner choice at the moment.
Add something to the browser's useragent string and look for that in my logic. Pretty easy on Firefox via about:config, but is less easy with the other browsers, and downright difficult on the Mac. Also, on some browsers, once you've set a custom value, you lose the ability to have the UA get auto-updated when you get a browser update. Your UA's version info is stuck in time to where it was when you first custom'ed it.
Can anyone think of another way that via email/IM/phone I can say to someone "do this" and they'll see the page behave differently for troubleshooting purposes. For the general population who haven't done that though, it's running completely normally.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的选择似乎是在您的站点上创建一个调试页面,让用户打开/关闭“调试”cookie,然后将常规站点代码从 cookie 的存在中排队。我建议让调试 cookie 在相当短的时间内过期,这样人们就不会无意中将其保留。
这个选项有你感兴趣的优点(没有用户代理修改,适用于所有平台,适用于 iframe),我看不出有什么缺点,除了如果有人关闭了 cookie 它就不起作用,尽管很多网络都赢了现在没有cookie就无法工作,所以这似乎是你可以忍受的东西。
此外,它对于用户来说也是最简单易用的。您只需将他们引导到您网站上的正确页面,他们所要做的就是单击所需的按钮来打开或关闭它。您可以使页面变得非常不言自明,这肯定比任何其他选项都要容易得多。
好的,如果您只控制代码而不控制 HTML,那么您可以实现一个键盘快捷键,通过设置 cookie 来启用调试模式。说明可能如下所示:将键盘焦点放在 X 上,然后按 Ctrl-D。
或者,您可以实现某种特殊类型的单击(例如按住 Ctrl 键单击某个对象或某个页面角落)。在这种情况下,说明可能类似于:按住 Ctrl 键并单击页面上的对象 X。
您的 JS 代码可以实现这些快捷方式中的任何一个。当按下特殊键/单击以确认打开或关闭调试模式时,您甚至可以发出提示(全部使用动态创建的 HTML 对象)。
The simplest option seems to be to make a debug page on your site that will let the user turn the "debug" cookie on/off and then queue your regular site code off the existence of the cookie. I'd suggest making the debug cookie expire in a fairly short amount of time so people don't inadvertently leave it on.
This option has the advantages you are interested in (no user agent modification, works on all platforms, works with iframes) and I can see no disadvantages except that it wouldn't work if someone had cookies off, though a lot of the web won't work without cookies these days so that seems like something you could live with.
In addition, it's also the simplest for your users to use. You can just direct them to the right page on your site and all they have to do is click the desired button to turn it on or off. You can make the page very self explanatory which is certainly much easier than any of the other options.
OK, if you only control code and no HTML, then you could do either implement a keyboard shortcut key that would enable the debug mode by setting the cookie. The instructions could be something like this: put the keyboard focus in X and then press Ctrl-D.
Or, you could implement some special type of click (like a Ctrl-click on some object or in some page corner). In this case the instructions could be something like: Hold down the Ctrl-key and click on object X on the page.
Your JS code could implement either of those shortcuts. You could even put up a prompt (all with dynamically created HTML objects) when the special key/click was engaged to confirm turning the debug mode on or off.