Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

php - How to make an Instant Shorten link for a url shortener

I have recently made a url shortener and I want to make an "Instant Shorten" Bookmark link on my site, like YOURLS can, so you bookmark the link, then go to any webpage, then you click on the bookmark and it automatically shortens the address, adds it to the database and tells you the shortened url in a pop-up.

I'm not sure how to do this, please help me!

Edit: This is the code that YOURLS currently uses to shorten using the bookmark link:

javascript:(function()%7Bvar%20d=document,s=d.createElement('script');window.yourls_callback=function(r)%7Bif(r.short_url)%7Bprompt(r.message,r.short_url);%7Delse%7Balert('An%20error%20occured:%20'+r.message);%7D%7D;s.src='http://nix-pix.co.uk/public/admin/index.php?u='+encodeURIComponent(d.location.href)+'&jsonp=yourls';void(d.body.appendChild(s));%7D)();

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Usually a bookmarklet something like this is used:

javascript:u=encodeURIComponent(location.href);s='http://urlshortener.com/shorten.php?url='+u;window.open(s,'shortened','location=no,width=400,height=300');

That takes the URL of the current page and opens a new window pointing to urlshortener.com/shorten.php?url=[the url to be shortened]. The code used by YOURLS is more complicated, but probably does approximately the same thing. You just need to change the URL that the new window opens to in the code above.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...