N
N
Nicholas2015-11-16 20:02:44
Twitter
Nicholas, 2015-11-16 20:02:44

How to fix Content Security Policy error when sharing a tweet?

Let's say we have a page with content and a button (link). When you click on the link, the https://twitter.com/intent/tweet window should open where you can share the page.
The problem is that the page did not work. The browser console is full of errors like:

Content Security Policy: The page's settings blocked the loading of a resource at self ("script-src https://connect.facebook.net https://cm.g.doubleclick.net https://ssl.google-analytics. com https://graph.facebook.com https://twitter.com https://*.userzoom.com 'unsafe-eval' https://*.twimg.com https://api.twitter.com ' nonce-L9Fi58Hr8JaO8qnbBZYNCA==' https://analytics.twitter.com https://ton.twitter.com https://syndication.twitter.com https://www.google.com https://t.tellapart. com https://platform.twitter.com https://www.google-analytics.com https://twitter.com ").
var twttr = twttr || {};
tw...

I found and read about what the Content Security Policy is and why it is needed, but I did not understand how to fix the situation.
PS An example of the page code (without php code and other husks)
<!DOCTYPE html>
<html lang="en">
<head>
  <script type="text/javascript" src="tw.js"></script>
</head>
<body>
    <a id="a" href="#">tweet</a>
    <div>Content ...</div>
</body>
</html>

tw.js code
function myListener(event) {
    window.open(
        'http://twitter.com/share?text=title&url=http://test.local/tw.php&counturl=http://test.local/tw.php',
        '',
        'toolbar=0,status=0,width=626,height=436'
    );
    event.preventDefault();
}
document.addEventListener(
    'DOMContentLoaded', 
    function () {
        document.getElementById("a").addEventListener("click", myListener, false);
    }, 
    false
);

PPS Thanks in advance for any hint.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question