如标题所述,我无法从我的Greasemonkey脚本中获得JIRA.bind()调用的工作,而且我已经不知道为什么以及要尝试其他方法了。
我在Firefox 50.1.0中运行JIRA 6.4.14和Greasemonkey 3.9。
如果我打开JIRA并在Firefox内置控制台中执行此行,则该行有效,并且在提交内联更改后显示“ GO”:
JIRA.bind(JIRA.Events.INLINE_EDIT_SAVE_COMPLETE, function(e, context, reason){alert("GO");})
因此,我认为将此命令移植到Greasemonkey中应该没问题:
unsafeWindow.JIRA.bind(unsafeWindow.JIRA.Events.INLINE_EDIT_SAVE_COMPLETE, function(e, context, reason){alert("GO");})
但是,当我执行完全相同的内联编辑时,什么也没发生。
该行本身已执行,我在前后“更改”了两个弹出窗口。
我尝试了通话的其他变体,但都没有成功
unsafeWindow.JIRA.bind(unsafeWindow.JIRA.Events.INLINE_EDIT_SAVE_COMPLETE, function(e, context, reason){ unsafeWindow.alert("GO");})
unsafeWindow.AJS.$(unsafeWindow.JIRA.bind(unsafeWindow.JIRA.Events.INLINE_EDIT_SAVE_COMPLETE, function(e, context, reason){alert("GO");}))
unsafeWindow.JIRA.bind(unsafeWindow.JIRA.Events.INLINE_EDIT_SAVE_COMPLETE, function(){ alert("GO");})
// While 'fooBar' is a simple function doing the alert("go")
unsafeWindow.JIRA.bind(unsafeWindow.JIRA.Events.INLINE_EDIT_SAVE_COMPLETE, function(){ fooBar })
有谁知道如何使绑定工作?
尝试exportFunction不能解决问题:
$(document).ready(function() {
unsafeWindow.JIRA.bind(unsafeWindow.JIRA.Events.INLINE_EDIT_SAVE_COMPLETE, function(e, context, reason){ foobar });
});
function foobar()
{
alert("GO");
}
exportFunction(foobar, unsafeWindow);
解:
感谢Brock Adams和wOxxOm的帮助!
此片段工作正常,并同时显示消息“绑定”和“运行”。
$(document).ready(function() {
// Write a log message from inside of the GM script
anotherMethod("Binding");
// Bind the exported foobar to the JIRA event
unsafeWindow.JIRA.bind(
unsafeWindow.JIRA.Events.INLINE_EDIT_SAVE_COMPLETE,
unsafeWindow.foobar
);
});
// Implementation of the foobar function
function foobar(e, context, reason)
{
anotherMethod("Go");
}
// Another method, that will get called from the GM script and the exported foobar
function anotherMethod(msg)
{
console.log(msg);
}
// Export foobar to the unsafeWindow to make it accessible for JIRA
unsafeWindow.foobar = exportFunction(foobar, unsafeWindow);
最佳答案
请参阅How to access `window` (Target page) objects when @grant values are set?。.bind()
调用中的所有内容都必须位于目标页面范围内,因此您不能使用像这样的动态function () {...}
代码。
像这样绑定您的回调:
function mySaveComplete (e, context, reason) {
//alert ("GO");
console.log ("Go");
}
unsafeWindow.mySaveComplete = exportFunction (mySaveComplete, unsafeWindow);
unsafeWindow.JIRA.bind (
unsafeWindow.JIRA.Events.INLINE_EDIT_SAVE_COMPLETE,
unsafeWindow.mySaveComplete
);
但是,我无法使用JIRA测试床。在某些情况下,您可能必须插入代码,作为链接的答案状态。
在这种情况下,另请参见:How to call Greasemonkey's GM_ functions from code that must run in the target page scope?
关于javascript - JIRA.bind()在Greasemonkey中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41725181/