Phonegap和提示()

aro*_*oth 9 javascript android prompt cordova

我正在浏览Android 的Phonegap源代码,并尝试验证他们的notification.alert()方法只是委托给本机JavaScript alert()函数.他们的代码做了:

Notification.prototype.alert = function(message, completeCallback, title, buttonLabel) {
    var _title = (title || "Alert");
    var _buttonLabel = (buttonLabel || "OK");
    PhoneGap.exec(completeCallback, null, "Notification", "alert", [message,_title,_buttonLabel]);
};
Run Code Online (Sandbox Code Playgroud)

在我看来像"alert"将被解释为要调用的函数的名称exec(),但是exec()正在执行:

PhoneGap.exec = function(success, fail, service, action, args) {
    try {
        var callbackId = service + PhoneGap.callbackId++;
        if (success || fail) {
            PhoneGap.callbacks[callbackId] = {success:success, fail:fail};
        }

        //using:  ["Notification", "alert", callbackId, true]
        var r = prompt(PhoneGap.stringify(args), 
                       "gap:"+PhoneGap.stringify([service, action, callbackId, true]));

        //...
    } catch (e2) {
        console.log("Error: "+e2);
    }
}; 
Run Code Online (Sandbox Code Playgroud)

现在PhoneGap.stringify()简单地解析为JSON.stringify(),所以Phonegap代码notification.alert()通过prompt()使用两个JSON对象/数组调用该函数来执行API方法.我的假设是prompt()机JavaScript prompt()函数(我没有在他们的JavaScript代码中找到任何会覆盖此函数的内容).如果是这种情况,那么这段代码是如何工作的呢?

它们也可以prompt()在其他各个地方使用:

PhoneGap.JSCallbackPort = prompt("getPort", "gap_callbackServer:"); 
Run Code Online (Sandbox Code Playgroud)

是否有一些特殊的关于他们调用的方式prompt()(特别是通过包含表单的第二个参数gap.*:.*)来启用一些自定义行为?或者他们以某种方式覆盖了prompt()JavaScript代码外部某处的函数的默认行为?

请注意,这特别适用于Android版本的Phonegap,因为其他版本似乎使用稍微不同的机制来执行API调用.

xda*_*azz 7

prompt()功能已被覆盖.

你可以在DroidGap.java中找到它.

@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
......
}
Run Code Online (Sandbox Code Playgroud)