Tech Note: Yet Another New Improved LGF Pages Bookmarklet
If you post LGF Pages, you may notice that your LGF Pages posting window is nagging you to install yet another update for the bookmarklet. This isn’t a bug fix or anything urgent, just some internal code improvements that will further refine the “separate loader and function library” design of the bookmarklet.
One small visible change: instead of opening in the center of the screen, the Create a Page window now opens at the top, 100 pixels from the left edge.
So you don’t technically need to update right away, but the posting window is going to keep nagging away at you until you do, so you might as well get it over with.
(Right click your old bookmarklet, delete it, then drag that big ol’ “Create a Page” button to your browser’s bookmarks bar in its place.)
Since I’ve been posting the source for these monsters, here’s the Javascript source for the latest version of the bookmarklet loader, and the separate function library.
First, the bookmarklet you install in your browser bar. It does only three major tasks: opens the popup “Create a Page” window, loads the function library, and loads the latest version of jQuery (if it isn’t already present). When everything is properly loaded, it then calls the function library’s “getQuery” method to do all the magic stuff (searching for embeddable videos and sound files in the opening page). getQuery returns a query string that is used to set the “href” attribute of the popup window, containing the page’s URL and title, any selected text, and any embedded video or audio it found.
(function() {
var w = 670,
h = 824,
popup = window.open(
'http://littlegreenfootballs.com/weblog/lgf-postpage.php?loading',
'_blank',
'scrollbars=1,resizable=1,width=' + w + ',height=' + h + ',left=100'
),
done = false,
s = document.createElement('script');
s.src = 'http://littlegreenfootballs.com/lgfjs/lgf-postpage-functions.min.js?' + (new Date()).getTime();
s.onload = s.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
done = true;
s.parentNode.removeChild(s);
loadjQuery();
}
};
document.getElementsByTagName('head')[0].appendChild(s);
function loadjQuery() {
var j = '1.10.2';
if (typeof(window.jQuery) === 'undefined' || window.jQuery.fn.jquery < j) {
var done = false,
s = document.createElement('script');
s.src = '//ajax.googleapis.com/ajax/libs/jquery/' + j + '/jquery.min.js';
s.onload = s.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
done = true;
s.parentNode.removeChild(s);
jQuery.noConflict();
postPage();
}
};
document.getElementsByTagName('head')[0].appendChild(s);
} else {
postPage();
}
}
function postPage() {
popup.location.href = 'http://littlegreenfootballs.com/weblog/lgf-postpage.php?' + $LGF_Page.getQuery() + '&v=1.2.1';
}
})();
This is the function library “lgf-postpage-functions.min.js,” which the bookmarklet loads by injecting a script tag into the
section of the opening page. This doesn’t hurt a bit.var $LGF_Page = {
version: '1.2.1',
getQuery: function() {
var esc = function(s) {
return escape(s).replace(/\+/ig, '%252B');
},
ob,
iframe = audio = '',
url = location.href,
d = document;
if (url.match(/www\.npr\.org/i)) {
ob = jQuery('a.download');
if (ob.length) {
audio = ob.eq(0).attr('href').split('?')[0];
}
} else if (url.match(/www\.rightwingwatch\.org/i)) {
ob = jQuery('#content-area object embed');
if (ob.length) {
if (ob.eq(0).attr('src').match(/www\.youtube\.com\/v\//i)) {
iframe = 'https://www.youtube.com/embed/' + ob.eq(0).attr('src').split('?')[0].split('/')[4];
}
} else {
ob = jQuery('#content-area iframe');
if (ob.length) {
iframe = ob.eq(0).attr('src');
if (iframe.substring(0, 2) == '//') {
iframe = 'http:' + iframe;
}
}
}
}
if (iframe == '') {
ob = jQuery('iframe').filter(function() {
return this.src.match(/https?:\/\/(www\.youtube|player\.vimeo\.|w\.soundcloud\.com\/player|www\.scribd\.com\/embeds\/|media\.mtvnservices\.com\/embed\/|mediamatters\.org\/embed\/static\/clips\/)/i);
});
if (ob.length && !url.match(/www\.youtube\.com\/|vimeo\.com\/|soundcloud\.com|www\.scribd\.com/i)) {
iframe = ob.eq(0).attr('src');
}
}
return (
'u=' + esc(url) +
'&t=' + esc(d.title) +
'&f=' + esc(iframe) +
'&a=' + esc(audio) +
'&s=' + esc(d.selection ? d.selection.createRange().text : d.getSelection())
);
}
};