Skip to content

Commit

Permalink
Merge pull request #292 from wbstack/improve-tinyurl
Browse files Browse the repository at this point in the history
Improve TinyURL shortener
  • Loading branch information
AndrewKostka authored Nov 4, 2024
2 parents b82d1bc + d8e423d commit 27f4b0e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
28 changes: 21 additions & 7 deletions wikibase/queryService/api/UrlShortener.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,28 @@ wikibase.queryService.api.UrlShortener = ( function ( $ ) {

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

return '<iframe ' +
'class="shortUrl" ' +
'src="' + TINYURL_API + encodeURIComponent( url ) + '" ' +
'referrerpolicy="origin" ' +
'sandbox="" ' +
'></iframe>';
var deferred = $.Deferred();
$.ajax( {
'method': 'POST',
'url': 'https://tinyurl.com/api-create.php',
'data': jQuery.param({ 'url': base64Url.toString() })
} ).done( function( text ) {
var text, html, $element;
html = '<!DOCTYPE html><meta charset="utf-8"><pre>' + htmlEscape( text ) + '</pre>';
$element = $( '<iframe>' ).attr( {
'class': 'shortUrl',
'src': 'data:text/html;charset=utf-8,' + encodeURI( html ),
'sandbox': ''
} );
deferred.resolve( $element );
} ).fail( function() {
deferred.resolve( wikibase.queryService.ui.i18n.getMessage( 'wdqs-app-urlshortener-failed' ) );
} );
return deferred;
};

/** @return {string} HTML */
Expand Down
7 changes: 7 additions & 0 deletions wikibase/queryService/ui/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,13 @@ wikibase.queryService.ui.App = ( function( $, window, _, Cookies, moment ) {
* @private
*/
SELF.prototype._initQuery = function() {
var url = new URL( window.location );
if ( url.searchParams.has( 'base64', true ) ) {
url.search = '';
url.hash = atob( url.hash.substring( 1 ) );
history.pushState( null, '', url.toString() );
}

if ( window.location.hash !== '' ) {
if ( location.hash.indexOf( '#result#' ) === 0 ) {
location.hash = location.hash.replace( '#result#', '#' );
Expand Down

0 comments on commit 27f4b0e

Please sign in to comment.