简体   繁体   中英

UIWebView - Enable Right Click on Webpages

I have a UIWebView in my app and all is working well, but certain annoying webpages have disabled the ability for the user to right click to select text and display the copy-paste menu controller. I need to re-enable these actions for the user on these webpages. I suspect the answer has something to do with injecting Javascript into the page on webViewDidFinishLoading: , but I'm not sure how to craft this Javascript. Does anyone have any ideas?

From what I've inspected on the webpage, I see that it is disabling selection in the CSS. They are loading it via an external stylesheet file, if that information helps.

<link rel="stylesheet" media="screen" type="text/css" href="/css/styleSheet.css?hash=174">

which contains

body {
    -webkit-user-select: none;
}

I've tried injecting the following, but it's not working.

NSString* css = @"\"body { -webkit-user-select: default; }\"";
NSString* js = [NSString stringWithFormat:
                @"var styleNode = document.createElement('style');\n"
                "styleNode.type = \"text/css\";\n"
                "var styleText = document.createTextNode(%@);\n"
                "styleNode.appendChild(styleText);\n"
                "document.getElementsByTagName('head')[0].appendChild(styleNode);\n",css];
[self.webView stringByEvaluatingJavaScriptFromString:js];

However, if I use desktop Safari Web Inspector on my UIWebView, and change the page's external CSS sheet to - webkit-user-select: to default , it works. I just need a way of being able to do this in my code. But if I add this line to the html page, below where the stylesheet is loaded, it does not work.

<link rel="stylesheet" media="screen" type="text/css" href="/css/styleSheet.css?hash=174">
<style type="text/css">body { background: #AAA; -webkit-user-select: default; }</style>

This what I'm seeing in Safari Web Inspector. For some reason, my style addition, made after the stylesheet, is still being overrun.

Safari Inspector

How can I change the -webkit-user-select: to default? I prefer an answer that uses Javascript as opposed to adding it to an htmlString and reloading the page.

SOLVED:

So changing the parameter to auto works, like in the following.

NSString* css = @"\"body { -webkit-user-select: auto; }\"";
NSString* js = [NSString stringWithFormat:
                @"var styleNode = document.createElement('style');\n"
                "styleNode.type = \"text/css\";\n"
                "var styleText = document.createTextNode(%@);\n"
                "styleNode.appendChild(styleText);\n"
                "document.getElementsByTagName('head')[0].appendChild(styleNode);\n",css];
[webView stringByEvaluatingJavaScriptFromString:js];

First "-webkit-user-select: auto | none | text;" webkitUserSelect Takes only this values. Then

Nsstring *OverrideCssRules = @"document.body.style.cssText += '-webkit-user-select: auto !important;'"; OR @"document.body.style.webkitUserSelect='auto !important'";
[self.webView stringByEvaluatingJavaScriptFromString:OverrideCssRules];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM