Links do not work inside WebView due to "X-Frame-Options" set to "SAMEORIGIN"

I have a Twitter timeline in WebView that is perfectly displayed. I followed this post as a guide. I currently do not want to entertain other options such as Twitter4J, although I appreciate that this is a good alternative.

My problem is that I canโ€™t click any links, images or interact with the timeline in any other way, except to scroll up and down. I get the following error. How to get around this?

05-29 19:09:10.887: I/chromium(13226): [INFO:CONSOLE(0)] "Refused to display 'https://mobile.twitter.com/XXXXXXXX/status/YYYYYYYYYY/photo/1' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.", source: https://twitter.com/XXXXXXXX/status/YYYYYYYYYY/photo/1 (0)

Here is my code:

// Locate the WebView in fragment_twitter.xml
    WebView tweetWebView = (WebView) V.findViewById(R.id.tweetWebView);

    // Settings for the WebView
    tweetWebView.setBackgroundColor(0);
    tweetWebView.getSettings().setJavaScriptEnabled(true);
    tweetWebView.getSettings().setDomStorageEnabled(true);

    // Load the WebView with the imageURL
    String timelineWidget = "<a class=\"twitter-timeline\" data-dnt=\"true\" href=\"https://twitter.com/XXXXXXXX\"  data-widget-id=\"SECRETNUMBER\">Tweets by @XXXXXXXX</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.async=true;js.src=p+\"://platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");</script>";                   
    tweetWebView.loadDataWithBaseURL("https://twitter.com", timelineWidget, "text/html", "UTF-8", null);
+4
source share
1 answer

, , : -, - ,

mWebView.setWebViewClient(new WebViewClient() {

    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {

        Logger.logD(TAG, "URL = " + url);
        if(mWebView.getVisibility() == View.VISIBLE && !HomeActivity.isPaused()) {
        //Open url in native browser
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(i);
            return null;
        }
        return super.shouldInterceptRequest(view, url);
    }


    @Override
    public void onPageFinished(WebView view, String url) {
       //show embedded webview and hide the progress bar
        mWebView.setVisibility(View.VISIBLE);
        mProgressBar.setVisibility(View.GONE);
        super.onPageFinished(view, url);
    }

});

@Override
protected void onResume() {
    super.onResume();
    isPaused  = false;
}

@Override
protected void onPause() {
    super.onPause();
    isPaused = true;
}

, ,

0

Source: https://habr.com/ru/post/1542539/


All Articles