Firefox书签:将函数公开给全局命名空间 [英] Firefox Bookmarklet: Exposing functions to the global namespace

查看:167
本文介绍了Firefox书签:将函数公开给全局命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个在Chrome中运行得很好的适度复杂的Bookmarklet,但是我无法在Firefox中使用它。当我在Firefox中运行我的Bookmarklet时,它会重定向到一个只显示 true 的新页面。

将原因缩小到一个非常具体的事情:当你向全局命名空间公开新的函数时,Firefox似乎并不喜欢。

如果是这种情况,你知道更多关于它?这是记录在任何地方,这样我可以了解更多关于它?小书签的细微差别似乎很少,我找不到任何相关的东西。第二个问题是,如果有任何已知的解决方法。



对于一些示例(记住,要测试它们只需将这些代码行复制到Firefox中的书签。然后在任何页面上运行它们。)



重定向书签
$ b

javascript:!function(){window.okay = {test:function(){}}}();
$ b

< a href =https://raw.github.com/jmeas/knockout-view/2.0.0/build/ko-view.min.js =nofollow>我正在进行的项目



非转向书签

javascript :!function(){window.okay = {test:!0}}();



感谢!



现在,我打算在Knockout中使用手动订阅来获得我需要的功能。虽然知道这个问题的答案仍然是很好的。

解决方案

它与暴露全局无关。它和你书签的最终评估值有关。



被评估的任何JS代码总是返回一个来自最后一行代码或最后一行的值块。

对于我而言,在Firefox中,您的两个示例都会重定向,因为两者都返回true。您可以直接在Firefox控制台上粘贴代码来测试。



此外...

 <!code>的javascript:函数(){...}(); 

这很奇怪。我从来没有见过这种模式。



这是目前最常见的书签模式:

 的javascript:(函数(){...})(); 

只要你不结束带有的匿名函数return ,该模式的计算结果为 undefined ,并且不会发生重定向。

实现相同的结果是始终使用 void(0); 作为最后一行代码。如果它是最后一行,那么整个脚本的计算结果为 undefined ,并且不是重定向发生。


I'm working on a moderately-complex Bookmarklet that works just fine in Chrome, but I can't get it to work in Firefox. When I run my Bookmarklet in Firefox it redirects to a new page that only says true on it.

I've narrowed the cause down to a very specific thing: Firefox doesn't seem to like when you expose new functions to the global namespace.

If this is the case, might you know more about it? Is this documented anywhere, such that I can learn more about it? Resources on the nuances of bookmarklet-building seem scarce, and I can't find anything relevant. A second question would be if there are there any known workarounds.

And for some examples (remember, to test them simply copy these lines of code into a bookmarklet in Firefox. Then run them on any page).

Redirecting bookmarklets

javascript:!function(){window.okay={test:function(){}}}();

The project I'm working on

A Non-redirecting bookmarklet

javascript:!function(){window.okay={test:!0}}();

Any thoughts? Thanks!

For now, I plan to use manual subscriptions in Knockout to get the functionality I need. It'd still be nice to know the answer to this question, though.

解决方案

It has nothing to do with exposing globals. It has everything to do with the final evaluated value of your bookmarklet.

Any JS code that is evaluated always returns a value which comes from the last line of code or the last block.

For me, in Firefox, both of your examples redirect because both return true. You can test this by pasting the code directly into Firefox console.

Furthermore...

javascript:!function(){...}();

This is weird. I've never seen this pattern.

This is the most common bookmarklet pattern these days:

javascript:(function(){...})();

As long as you don't end that anonymous function with a return, that pattern evaluates to undefined, and no redirect happens.

The older way of achieving the same result was to always use void(0); as the last line of code. That evaluates to undefined also, and if it is the last line, then the entire script evaluates to undefined, and no redirect happens.

这篇关于Firefox书签:将函数公开给全局命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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