什么可以阻止Array.prototype.filter()在控制台中运行良好的防油精脚本中运行
What could stop Array.prototype.filter() from running in greasemonkey script that runs fine in console
以下是代码:
function fil(val) {
console.log('fil'); // never written to console when run in greasemonkey
return true;
}
var temp = unsafeWindow.someobject;
console.log(temp); // looks fine
temp.filter(fil); // never happens in greasemonkey
如果我在firebug控制台的同一个对象上运行完全相同的代码(当然没有unsafeWindow),它的输出就很好。原因可能是什么?
编辑以澄清:当在润滑器中运行时,字符串"fil"从未写入控制台,这表明从未调用过fil()。此外,如果我做了类似console.log(temp.filter(fil));
的事情,当代码从greatemonkey运行时,我永远不会在控制台中看到结果。(尽管我知道应该这样做,因为上面的行告诉temp
存在,并且代码一直运行到那个点。
在我的例子中,在Firefox 45和Greasemonkey 3.7中,这段代码运行良好:
// ==UserScript==
// @name Array.prototype.filter() test
// @namespace http://stackoverflow.com/questions/33675675/
// @include http://stackoverflow.com/questions/33675675/*
// @version 1.0
// @grant none
// ==/UserScript==
function fil(val) {
console.log('fil'); // never written to console when run in greasemonkey
return true;
}
var temp = unsafeWindow.allowedHosts;
console.log(temp); // looks fine
temp.filter(fil); // never happens in greasemonkey
输出:
Array [ "stackoverflow.com", "serverfault.com" ]
fil
fil
但是将none
更改为,例如,第行中的GM_getValue
:
// @grant none
仅输出:
Array [ "stackoverflow.com", "serverfault.com" ]
这是因为向@grant
传递除none
之外的任何值都会启用沙箱,而且从我所看到的,封装的对象不支持将回调函数作为参数的方法。
您可以用上面链接中的兼容层替换一些GM_*
权限。然后你可以通过设置@grant none
来禁用沙箱,你就做好了准备。
但是,如果您需要原始函数,可以使用[].filter.call(temp, fil)
而不是temp.filter(fil)
,如注释中所述。
相关文章:
- 如何在加载网页时运行脚本
- 使用JavaScript运行脚本
- javascript动态内容与选择长运行脚本
- Javascript:在新页面中打开链接并运行脚本
- 如何在内部脚本完全加载后运行脚本
- 对couchdb数据库/视图中的所有文档运行脚本/查询
- 单击1个表单中的2个提交按钮中的1个时,如何运行脚本
- 是否可以在node.js依赖项上运行脚本
- "npm运行构建:css"不能工作,而当我自己运行脚本时是可以的
- 运行脚本以禁用脚本
- 在网络whatsapp上运行脚本
- 仅在 url 索引上运行脚本
- [XSS]Expressjs 阻止从 POST 请求运行脚本
- 在触发无限滚动时运行脚本
- 使用Browserify运行脚本,无需先进行构建过程
- "停止运行脚本错误“;在IE8中拥有大量收藏
- PHP-如何让别人可以'不要更改url中的参数,也不要在刷新页面时再次运行脚本
- 如何在不阻塞脚本的情况下运行脚本
- 如果用户登录不起作用,则运行脚本
- 我想在用Jquery.ajax方法加载数据后在Html中运行脚本