Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Jockeyvb opened this issue Apr 29, 2019 · 0 comments
Open

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

Jockeyvb opened this issue Apr 29, 2019 · 0 comments

Comments

@Jockeyvb
Copy link

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 或报错。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant