Xam.Plugin.Webview icon indicating copy to clipboard operation
Xam.Plugin.Webview copied to clipboard

js 注入时机太迟,做APP时必须提早注入。

Open Jockeyvb opened this issue 5 years ago • 0 comments

internal async Task OnJavascriptInjectionRequest(string js) { if (Control == null || Element == null) return string.Empty;

        var response = string.Empty;

        try
        {
            var script = new WKUserScript(new NSString(js), WKUserScriptInjectionTime.AtDocumentStart, false);
            if (!_contentController.UserScripts.Contains(script)) { 
            _contentController.AddUserScript(script);
            _configuration.UserContentController = _contentController;
            }
            //var obj = await Control.EvaluateJavaScriptAsync(js).ConfigureAwait(false);
            //if (obj != null)
            //    response = obj.ToString();
        }

        catch (Exception) { /* The Webview might not be ready... */ }
        return response;
	}

代码直接写入到WKUserScriptInjectionTime.AtDocumentStart

然后在html 中,这样$(function(){ alert(jsCallAPP));} 则可以响应,如果用Control.EvaluateJavaScriptAsync(js)则在onready 事件中也未注入,所以为undefine 或报错。

Jockeyvb avatar Apr 29 '19 05:04 Jockeyvb