Skip to content

Commit 27f4b0e

Browse files
authored
Merge pull request #292 from wbstack/improve-tinyurl
Improve TinyURL shortener
2 parents b82d1bc + d8e423d commit 27f4b0e

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

wikibase/queryService/api/UrlShortener.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,28 @@ wikibase.queryService.api.UrlShortener = ( function ( $ ) {
8484

8585
/** @return {string} HTML */
8686
SELF.prototype._getTinyUrl = function( url ) {
87-
var TINYURL_API = '//tinyurl.com/api-create.php?url=';
87+
var base64Url = new URL( url );
88+
base64Url.searchParams.append( 'base64', true );
89+
base64Url.hash = btoa( base64Url.hash );
8890

89-
return '<iframe ' +
90-
'class="shortUrl" ' +
91-
'src="' + TINYURL_API + encodeURIComponent( url ) + '" ' +
92-
'referrerpolicy="origin" ' +
93-
'sandbox="" ' +
94-
'></iframe>';
91+
var deferred = $.Deferred();
92+
$.ajax( {
93+
'method': 'POST',
94+
'url': 'https://tinyurl.com/api-create.php',
95+
'data': jQuery.param({ 'url': base64Url.toString() })
96+
} ).done( function( text ) {
97+
var text, html, $element;
98+
html = '<!DOCTYPE html><meta charset="utf-8"><pre>' + htmlEscape( text ) + '</pre>';
99+
$element = $( '<iframe>' ).attr( {
100+
'class': 'shortUrl',
101+
'src': 'data:text/html;charset=utf-8,' + encodeURI( html ),
102+
'sandbox': ''
103+
} );
104+
deferred.resolve( $element );
105+
} ).fail( function() {
106+
deferred.resolve( wikibase.queryService.ui.i18n.getMessage( 'wdqs-app-urlshortener-failed' ) );
107+
} );
108+
return deferred;
95109
};
96110

97111
/** @return {string} HTML */

wikibase/queryService/ui/App.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,13 @@ wikibase.queryService.ui.App = ( function( $, window, _, Cookies, moment ) {
588588
* @private
589589
*/
590590
SELF.prototype._initQuery = function() {
591+
var url = new URL( window.location );
592+
if ( url.searchParams.has( 'base64', true ) ) {
593+
url.search = '';
594+
url.hash = atob( url.hash.substring( 1 ) );
595+
history.pushState( null, '', url.toString() );
596+
}
597+
591598
if ( window.location.hash !== '' ) {
592599
if ( location.hash.indexOf( '#result#' ) === 0 ) {
593600
location.hash = location.hash.replace( '#result#', '#' );

0 commit comments

Comments
 (0)