Tech Note: The Latest LGF Pages Bookmarklet, With Pop-Up Blocker Detection
On weekends my inner code monkey runs free, and that dastardly marauding primate has produced a new version of the LGF Pages “Create a Page” bookmarklet again tonight. The new feature: better handling of pop-up blockers.
All the major web browsers are installed these days with pop-up window blocking enabled, and it’s the number one reason for problems getting the LGF Pages bookmarklet to work. Previous versions of the bookmarklet would simply do nothing if pop-up blocking was on; you’d click the button and nothing would happen. Not very helpful in figuring out what’s wrong.
Now I’ve baked in some code that detects whether the LGF Pages posting window has opened, and if it hasn’t you’ll see a message with a polite, obsequious request to turn off your browser’s pop-up blocking. Much more helpful and informative.
Here’s the latest Javascript source — on the coding side I’ve cleaned up and re-scoped the way functions and variables are defined to keep them neatly packaged within the sections where they are used. I usually name variables a little more expansively, but since it’s a bookmarklet space is at a premium, so most of the variables are single letters. If you read the code, it’s pretty obvious what most of them do; ‘v’ is the version, ‘u’ is the page’s URL, etc.
(function() {
var postPage = function() {
var api = 'http://littlegreenfootballs.com/api/0.2/postRegex.json',
opts = {
url: api
},
browser = navigator.userAgent.toLowerCase(),
ff = browser.match(/firefox\/(.*)$/);
if (ff && ff.length > 1 && ff[1] < 21) {
opts = {
url: api + 'p',
jsonpCallback: 'lgf_api',
dataType: 'jsonp'
};
}
jQuery.ajax(opts).done(function(lgf) {
var esc = function(s) {
return escape(s).replace(/\+/ig, '%252B');
},
popCheck = function(popup) {
if (!popup.innerHeight) {
popError();
}
},
popError = function() {
alert('To use the LGF Pages bookmarklet, please turn off pop-up blocking.');
},
popup,
v = '1.0.3',
f = a = q = w = '',
u = esc(location.href),
d = document,
t = esc(d.title),
s = esc((d.selection) ? d.selection.createRange().text : d.getSelection()),
ob = jQuery('iframe').filter(function() {
return this.src.match(RegExp(lgf.regex.embed, 'i'));
});
if (ob.length && !u.match(RegExp(lgf.regex.sites, 'i'))) {
f = esc(ob.eq(0).attr('src'));
}
if (u.match(/www\.npr\.org/i)) {
ob = jQuery('a.download');
if (ob.length) {
a = esc(ob.eq(0).attr('href').split('?')[0]);
}
}
q = (
'u=' + u +
'&t=' + t +
'&f=' + f +
'&a=' + a +
'&s=' + s +
'&v=' + v
);
w = (
'width=670,' +
'height=820,' +
'scrollbars=1,' +
'status=1,' +
'menubar=1,' +
'location=1,' +
'resizable=1'
);
if (popup = window.open('http://littlegreenfootballs.com/weblog/lgf-postpage.php?' + q, '_blank', w)) {
if (/chrome/.test(browser)) {
setTimeout(function() {
popCheck(popup);
}, 200);
} else {
popup.onload = function() {
popCheck(popup);
};
}
} else {
popError();
}
});
},
jqVersion = '1.9.1',
d = document;
if (window.jQuery === undefined || window.jQuery.fn.jquery < jqVersion) {
var done = false, s = d.createElement('script');
s.src = '//ajax.googleapis.com/ajax/libs/jquery/' + jqVersion + '/jquery.min.js';
s.onload = s.onreadystatechange = function(){
if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
done = true;
jQuery.noConflict();
postPage();
}
};
d.getElementsByTagName('head')[0].appendChild(s);
} else {
postPage();
}
})();
And the highly compressed Closure Compiler one-line version that becomes your bookmarklet:
javascript:(function(){var g=function(){var b={url:"http://littlegreenfootballs.com/api/0.2/postRegex.json"},g=navigator.userAgent.toLowerCase(),c=g.match(/firefox\/(.*)$/);c&&(1c[1])&&(b={url:"http://littlegreenfootballs.com/api/0.2/postRegex.jsonp",jsonpCallback:"lgf_api",dataType:"jsonp"});jQuery.ajax(b).done(function(b){var e=function(b){return escape(b).replace(/\+/ig,"%252B")},c=function(){alert("To use the LGF Pages bookmarklet, please turn off pop-up blocking.")},k,h=a=q=w="",m=e(location.href), d=document,l=e(d.title),d=e(d.selection?d.selection.createRange().text:d.getSelection()),f=jQuery("iframe").filter(function(){return this.src.match(RegExp(b.regex.embed,"i"))});f.length&&!m.match(RegExp(b.regex.sites,"i"))&&(h=e(f.eq(0).attr("src")));m.match(/www\.npr\.org/i)&&(f=jQuery("a.download"),f.length&&(a=e(f.eq(0).attr("href").split("?")[0])));q="u="+m+"&t="+l+"&f="+h+"&a="+a+"&s="+d+"&v=1.0.3";w="width=670,height=820,scrollbars=1,status=1,menubar=1,location=1,resizable=1";(k=window.open("http://littlegreenfootballs.com/weblog/lgf-postpage.php?"+ q,"_blank",w))?/chrome/.test(g)?setTimeout(function(){k.innerHeight||c()},200):k.onload=function(){k.innerHeight||c()}:c()})},h=document;if(void 0===window.jQuery||"1.9.1">window.jQuery.fn.jquery){var l=!1,b=h.createElement("script");b.src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js";b.onload=b.onreadystatechange=function(){if(!l&&(!this.readyState||"loaded"==this.readyState||"complete"==this.readyState))l=!0,jQuery.noConflict(),g()};h.getElementsByTagName("head")[0].appendChild(b)}else g()})();