admin管理员组文章数量:1289421
I want to inject a script using WKWebview
API. For some reason it isn't working and I can't figure it out. I've tried debugging in Safari
developer console and I can't find the JavaScript
code in there either.
Implementation Code as follows:
NSString *js = @"document.body.style.background = \"#FF0000\";";
NSString *myScriptSource = @"alert('Hello, World!')";
WKUserScript *s = [[WKUserScript alloc] initWithSource:myScriptSource injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
WKUserContentController *c = [[WKUserContentController alloc] init];
[c addUserScript:s];
WKWebViewConfiguration *conf = [[WKWebViewConfiguration alloc] init];
conf.userContentController = c;
WKWebView *webview = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:conf];
[self.view addSubview:webview];
webview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Do any additional setup after loading the view, typically from a nib.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@""]];
[webview loadRequest:request];
});
I want to inject a script using WKWebview
API. For some reason it isn't working and I can't figure it out. I've tried debugging in Safari
developer console and I can't find the JavaScript
code in there either.
Implementation Code as follows:
NSString *js = @"document.body.style.background = \"#FF0000\";";
NSString *myScriptSource = @"alert('Hello, World!')";
WKUserScript *s = [[WKUserScript alloc] initWithSource:myScriptSource injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
WKUserContentController *c = [[WKUserContentController alloc] init];
[c addUserScript:s];
WKWebViewConfiguration *conf = [[WKWebViewConfiguration alloc] init];
conf.userContentController = c;
WKWebView *webview = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:conf];
[self.view addSubview:webview];
webview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Do any additional setup after loading the view, typically from a nib.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google."]];
[webview loadRequest:request];
});
Share
Improve this question
asked Oct 5, 2014 at 13:17
AvbaAvba
15.3k21 gold badges105 silver badges213 bronze badges
2
- 2 So, have you found an answer to your question? – Bastian Commented Jan 21, 2016 at 9:58
- @Bastian can you try with my sollution if this will help you – Anurag Soni Commented May 17, 2017 at 6:05
2 Answers
Reset to default 4alert
isn't implemented in WKWebView
by default, so even if your user script runs it won't visibly do anything. You need to implement runJavaScriptAlertPanelWithMessage
:
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, pletionHandler: @escaping (() -> Void)) {
let alert = UIAlertController.create(title: frame.request.url?.host, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
pletionHandler()
}))
present(alert, animated: true, pletion: nil)
}
I don't think the injected JavaScript actually appears in the DOM anywhere, it's a property internal to the WKWebView
.
Please use code like this and add script message handler and set navigation delegate
NSString *js = @"document.body.style.background = \"#FF0000\";";
NSString *myScriptSource = @"alert('Hello, World!')";
WKUserScript *s = [[WKUserScript alloc] initWithSource:myScriptSource injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
WKUserContentController *c = [[WKUserContentController alloc] init];
[c addUserScript:s];
// Add a script message handler for receiving "buttonClicked" event notifications posted from the JS document using window.webkit.messageHandlers.buttonClicked.postMessage script message
[c addScriptMessageHandler:self name:@"buttonClicked"];
WKWebViewConfiguration *conf = [[WKWebViewConfiguration alloc] init];
conf.userContentController = c;
WKWebView *webview = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:conf];
[self.view addSubview:webview];
webview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Do any additional setup after loading the view, typically from a nib.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google."]];
[webview loadRequest:request];
});
implement script message handler "WKScriptMessageHandler" with method name
#pragma mark -WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if ([message.name isEqualToString:@"buttonClicked"]) {
self.buttonClicked ++;
}
// JS objects are automatically mapped to ObjC objects
id messageBody = message.body;
if ([messageBody isKindOfClass:[NSDictionary class]]) {
NSString* idOfTappedButton = messageBody[@"ButtonId"];
[self updateColorOfButtonWithId:idOfTappedButton];
}
}
and post the message form js like this
var button = document.getElementById("clickMeButton");
button.addEventListener("click", function() {
var messgeToPost = {'ButtonId':'clickMeButton'};
window.webkit.messageHandlers.buttonClicked.postMessage(messgeToPost);
},false);
you will receive a call back
本文标签: javascriptWKUserScript not workingStack Overflow
版权声明:本文标题:javascript - WKUserScript not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741462043a2380093.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论