From 0549bb51131539decd6ba9f50a01770699f65009 Mon Sep 17 00:00:00 2001 From: Lapo Luchini Date: Wed, 28 May 2014 15:47:55 +0200 Subject: [PATCH 1/4] Use jQuery as soon as it is loaded. --- pinry/static/js/bookmarklet.js | 200 +++++++++++++++++---------------- 1 file changed, 101 insertions(+), 99 deletions(-) diff --git a/pinry/static/js/bookmarklet.js b/pinry/static/js/bookmarklet.js index b7cc539..e6abe5c 100644 --- a/pinry/static/js/bookmarklet.js +++ b/pinry/static/js/bookmarklet.js @@ -14,107 +14,109 @@ if (!window.jQuery) { var body = document.getElementsByTagName('body')[0]; var script = document.createElement('script'); + script.onload = main; script.src = '//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js'; body.appendChild(script); -} +} else + jQuery(document).ready(main); // End jQuery Check -(function($) { - $(document).ready(function() { - // Start Helper Functions - function getFormUrl() { - var hostUrl = $('#pinry-bookmarklet').attr('src').split('/')[2]; - var formUrl = '/pins/pin-form/?pin-image-url='; - return 'http://'+hostUrl+formUrl; +function main() { + var $ = jQuery; + + // Start Helper Functions + function getFormUrl() { + var hostUrl = $('#pinry-bookmarklet').attr('src').split('/')[2]; + var formUrl = '/pins/pin-form/?pin-image-url='; + return 'http://'+hostUrl+formUrl; + } + + function normalizeImageUrl(imageUrl) { + var protocol = imageUrl.split(':')[0]; + if (protocol != 'http' && protocol != 'https') { + if (imageUrl[1] != '/') + imageUrl = 'http://'+window.location.host+imageUrl; } - - function normalizeImageUrl(imageUrl) { - var protocol = imageUrl.split(':')[0]; - if (protocol != 'http' && protocol != 'https') { - if (imageUrl[1] != '/') - imageUrl = 'http://'+window.location.host+imageUrl; - } - return imageUrl; - } - // End Helper Functions - - - // Start View Functions - function pageView() { - var pinryImages = document.createElement('div'); - pinryImages.id = 'pinry-images'; - $(pinryImages).css({ - 'position': 'absolute', - 'z-index': '9001', - 'background': 'rgba(0, 0, 0, 0.7)', - 'padding-top': '70px', - 'top': '0', - 'left': '0', - 'right': '0', - 'height': $(document).height(), - 'text-align': 'center', - 'width': '100%' - }); - var pinryBar = document.createElement('div'); - pinryBar.id = 'pinry-bar'; - $(pinryBar).css({ - 'background': 'black', - 'padding': '15px', - 'position': 'absolute', - 'z-index': '9002', - 'width': '100%', - 'top': 0, - 'border-bottom': '1px solid #555', - 'color': 'white', - 'text-align': 'center', - 'font-size': '22px' - }); - $('body').append(pinryImages); - $('#pinry-images').append(pinryBar); - $('#pinry-bar').html('Pinry Bookmarklet'); - $(window).scrollTop(0); - } - - function imageView(imageUrl) { - // Requires that pageView has been created already - imageUrl = normalizeImageUrl(imageUrl); - var image = document.createElement('div'); - $(image).css({ - 'background-image': 'url('+imageUrl+')', - 'background-position': 'center center', - 'background-repeat': 'no-repeat', - 'display': 'inline-block', - 'width': '200px', - 'height': '200px', - 'margin': '15px', - 'cursor': 'pointer', - 'border': '1px solid #555' - }); - $(image).click(function() { - var popUrl = getFormUrl()+imageUrl; - window.open(popUrl); - $('#pinry-images').remove(); - }); - return $('#pinry-images').append(image); - } - // End View Functions - - - // Start Active Functions - function addAllImagesToPageView() { - var images = $('body').find('img'); - images.each(function() { - if ($(this).width() > 200 && $(this).height() > 200) - imageView($(this).attr('src')); - }); - return images; - } - // End Active Functions - - - // Start Init - pageView(); // Build page before we insert images - addAllImagesToPageView(); // Add all images on page to our new pageView - // End Init - }); -})(jQuery); + return imageUrl; + } + // End Helper Functions + + + // Start View Functions + function pageView() { + var pinryImages = document.createElement('div'); + pinryImages.id = 'pinry-images'; + $(pinryImages).css({ + 'position': 'absolute', + 'z-index': '9001', + 'background': 'rgba(0, 0, 0, 0.7)', + 'padding-top': '70px', + 'top': '0', + 'left': '0', + 'right': '0', + 'height': $(document).height(), + 'text-align': 'center', + 'width': '100%' + }); + var pinryBar = document.createElement('div'); + pinryBar.id = 'pinry-bar'; + $(pinryBar).css({ + 'background': 'black', + 'padding': '15px', + 'position': 'absolute', + 'z-index': '9002', + 'width': '100%', + 'top': 0, + 'border-bottom': '1px solid #555', + 'color': 'white', + 'text-align': 'center', + 'font-size': '22px' + }); + $('body').append(pinryImages); + $('#pinry-images').append(pinryBar); + $('#pinry-bar').html('Pinry Bookmarklet'); + $(window).scrollTop(0); + } + + function imageView(imageUrl) { + // Requires that pageView has been created already + imageUrl = normalizeImageUrl(imageUrl); + var image = document.createElement('div'); + $(image).css({ + 'background-image': 'url('+imageUrl+')', + 'background-position': 'center center', + 'background-repeat': 'no-repeat', + 'display': 'inline-block', + 'width': '200px', + 'height': '200px', + 'margin': '15px', + 'cursor': 'pointer', + 'border': '1px solid #555' + }); + $(image).click(function() { + var popUrl = getFormUrl()+imageUrl; + window.open(popUrl); + $('#pinry-images').remove(); + }); + return $('#pinry-images').append(image); + } + // End View Functions + + + // Start Active Functions + function addAllImagesToPageView() { + var images = $('body').find('img'); + images.each(function() { + if ($(this).width() > 200 && $(this).height() > 200) + imageView($(this).attr('src')); + }); + return images; + } + // End Active Functions + + + // Start Init + pageView(); // Build page before we insert images + addAllImagesToPageView(); // Add all images on page to our new pageView + // End Init +} From a2ab9a2b8cdbef7bb531ed7dea2d571cf214b327 Mon Sep 17 00:00:00 2001 From: Lapo Luchini Date: Wed, 28 May 2014 15:58:00 +0200 Subject: [PATCH 2/4] Use `.src` to get absolute URL as calculated by the browser. --- pinry/static/js/bookmarklet.js | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/pinry/static/js/bookmarklet.js b/pinry/static/js/bookmarklet.js index e6abe5c..380a97e 100644 --- a/pinry/static/js/bookmarklet.js +++ b/pinry/static/js/bookmarklet.js @@ -30,15 +30,6 @@ function main() { var formUrl = '/pins/pin-form/?pin-image-url='; return 'http://'+hostUrl+formUrl; } - - function normalizeImageUrl(imageUrl) { - var protocol = imageUrl.split(':')[0]; - if (protocol != 'http' && protocol != 'https') { - if (imageUrl[1] != '/') - imageUrl = 'http://'+window.location.host+imageUrl; - } - return imageUrl; - } // End Helper Functions @@ -80,7 +71,6 @@ function main() { function imageView(imageUrl) { // Requires that pageView has been created already - imageUrl = normalizeImageUrl(imageUrl); var image = document.createElement('div'); $(image).css({ 'background-image': 'url('+imageUrl+')', @@ -108,7 +98,7 @@ function main() { var images = $('body').find('img'); images.each(function() { if ($(this).width() > 200 && $(this).height() > 200) - imageView($(this).attr('src')); + imageView(this.src); }); return images; } From e1e2c07f2b11c27d3fa040a9b406e4f3adf9d17b Mon Sep 17 00:00:00 2001 From: Lapo Luchini Date: Wed, 28 May 2014 16:09:22 +0200 Subject: [PATCH 3/4] `getUrlParameter` fixes as in http://stackoverflow.com/a/8764051/166524 --- pinry/static/js/helpers.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pinry/static/js/helpers.js b/pinry/static/js/helpers.js index e82f1a0..33e95e8 100644 --- a/pinry/static/js/helpers.js +++ b/pinry/static/js/helpers.js @@ -56,9 +56,5 @@ function postPinData(data) { function getUrlParameter(name) { - var decode = decodeURI( - (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1] - ); - if (decode == 'null') return null; - else return decode; + return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null; } From e2f61176ddcefa2a3b11ad7f869f36edf71c1386 Mon Sep 17 00:00:00 2001 From: Lapo Luchini Date: Wed, 28 May 2014 16:10:08 +0200 Subject: [PATCH 4/4] Properly encode query string parameters. --- pinry/static/js/bookmarklet.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pinry/static/js/bookmarklet.js b/pinry/static/js/bookmarklet.js index 380a97e..394cf96 100644 --- a/pinry/static/js/bookmarklet.js +++ b/pinry/static/js/bookmarklet.js @@ -84,7 +84,7 @@ function main() { 'border': '1px solid #555' }); $(image).click(function() { - var popUrl = getFormUrl()+imageUrl; + var popUrl = getFormUrl()+encodeURIComponent(imageUrl); window.open(popUrl); $('#pinry-images').remove(); });