简体   繁体   中英

Twitter web intents URL shortener

I'm trying to user Twitter Web Intents to add a custom tweet functionality to my page. This is a complex page with a lot of query parameters and it goes way over 140 characters. The URL is not getting shortened.

This is the function I'm using:

function tweet(image){
    var pageURL = createPreloadURL(image)
    var width  = 575,
        height = 400,
        left   = ($(window).width()  - width)  / 2,
        top    = ($(window).height() - height) / 2,
        url    = 'https://twitter.com/intent/tweet?url='+ encodeURIComponent(pageURL),
        opts   = 'status=1' +
         ',width='  + width  +
         ',height=' + height +
         ',top='    + top    +
         ',left='   + left;
        window.open(url, 'twitter', opts);
}

I consoled out the URL string and it seems to be in the right format:

http%3A%2F%2Fexample.com%2Fpreload.cfm%3Fproduct_id%3D4420%26year%3D2014%26make_id%3D58%26model_id%3D21604%26diameter%3D17....

On the documentation here , it the says:

A fully-qualified URL with a HTTP or HTTPS scheme, URL-encoded. The provided URL will be shortened with Twitter's t.co to the number of characters specified by short_url_length.

Example Value: http:%3A%2F%2Fexample.com%2F

I looked in the console on the new window I'm getting this error

Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'unsafe-inline' 'nonce-PfLsh9SlUoh7UKtdZ7mgcA==' 'unsafe-eval' https:". Note that 'unsafe-inline' is ignored if either a hash or nonce value is present in the source list.

Are there additional steps I need to take to make the url shortener work?

I found the problem I was having. I was working on a dev link that was an IP address. Twitter's URL shortener apparently doesn't like that. When I tested it on a regular URL, the URL shortened.

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