简体   繁体   English

UIWebView - 在网页上启用右键单击

[英]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.我的应用程序中有一个 UIWebView 并且一切正常,但是某些烦人的网页禁用了用户右键单击以选择文本并显示复制粘贴菜单控制器的能力。 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.我怀疑答案与在webViewDidFinishLoading: Javascript 注入页面有关,但我不确定如何制作此 Javascript。 Does anyone have any ideas?有没有人有任何想法?

From what I've inspected on the webpage, I see that it is disabling selection in the CSS.从我在网页上检查的内容来看,我看到它正在禁用 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.但是,如果我在 UIWebView 上使用桌面 Safari Web Inspector,并将页面的外部 CSS 表更改为 - webkit-user-select: to default ,则它可以工作。 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.但是,如果我将此行添加到 html 页面,在加载样式表的下方,它不起作用。

<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.这是我在 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?如何将-webkit-user-select:更改为默认值? I prefer an answer that uses Javascript as opposed to adding it to an htmlString and reloading the page.我更喜欢使用 Javascript 的答案,而不是将其添加到 htmlString 并重新加载页面。

SOLVED:解决了:

So changing the parameter to auto works, like in the following.因此,将参数更改为auto有效,如下所示。

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;"第一个"-webkit-user-select: auto | none | text;" webkitUserSelect Takes only this values. webkitUserSelect 仅采用此值。 Then然后

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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