Android WebView ignoring target=“_blank” when added WebViewClient

爱⌒轻易说出口 提交于 2019-11-30 13:18:13
rekire

I have an idea for a workaround. Replace all links which should been opened in a new window with a custom schema. Then you can handle that by your own.

For a minimal interruption set also a custom theme and handle all configuration changes:

<activity android:name="com.example.LinkHandler"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
          android:theme="@android:style/Theme.Translucent">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>

        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>

        <data android:scheme="your.link.handler.schema"/>
    </intent-filter>
</activity>

Then in your link handler you can read the url by using getIntent().getData().

Please also keep in mind that you should handle http and https, I skipped that in my short example above.

Here is an JavaScript example how to rewrite the urls:

for(var i=0; i<document.links.length; i++) {
    if(document.links[i].target == "_blank") {
        document.links[i].href = "your.schema.for."+document.links[i].href;
    }
}
<ul>
    <li><a href="http://example.com">Test 1</a></li>
    <li><a href="http://example.net" target="_blank">Test 2</a></li>
    <li><a href="https://example.com" target="_blank">Test 3</a></li>
    <li><a href="https://example.net">Test 4</a></li>
</ul>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!