JIRA.bind()在Greasemonkey中不起作用

时间:2017-01-18 16:57:35

标签: javascript jira greasemonkey

正如标题所说,我无法通过我的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的帮助!

这段剪辑正常工作并打印两条消息" Binding"和"去"。

$(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);

1 个答案:

答案 0 :(得分:1)

请参阅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?