Tech Note: Yet Another LGF Pages Bookmarklet Update

But this version will make updating much easier
LGF • Views: 20,261

I know, right? Another one, just a day later? But this one actually will make it possible for me to update the types of auto-embed content the LGF Pages bookmarklet can identify — without making you re-install the bookmarklet to take advantage of the new features.

When you open this new version of the bookmarklet, it “phones home” to littlegreenfootballs.com no matter which site you’re on, and gets a file with the information it needs to find embedded video and audio in the current page. The cool thing about doing it this way is that it lets me add new embed types to the file on our server, and the bookmarklet automatically starts using them the next time you open it — no reinstallation necessary any more!

To get the new extensible version of the bookmarklet, reinstall it one more time by first deleting the old one (either by dragging it off the bookmark bar or by right-clicking and choosing “Delete”), then dragging the button labeled “Create a Page” (at the top of the right sidebar) to your bookmark bar in its place.

A trouble-shooting tip: if you click the bookmarklet and no window (or tab) opens, it’s probably because popups have been blocked by the browser. Most browsers have popup blocking on by default. The easiest way to handle this is to turn popup blocking off entirely; you can also allow popups on specific sites in the browser’s Preferences, but for posting purposes it’s a nuisance to do this for every site you might want to post something about.

The technical details

This new bookmarklet is actually calling our brand spanking new API (version 0.1, a one-day old baby), using the JSONP technique to get around the Javascript same-origin policy that would normally prevent this kind of cross-site access.

This happens in the jQuery.ajax() method beginning at line 27. It makes a call to the API at:

littlegreenfootballs.com

jQuery handles all the details of the JSONP call (with its necessary callback function), parses the JSON response, and returns the data in the object lgf — in this case it consists of two regular expression definitions that are used on lines 34 and 36.

UPDATE at 5/18/13 5:08:19 pm

Stop the presses! Wouldn’t you know that as soon as I post this new version, I discover an even better way to get this done? Instead of JSONP, the bookmarklet now uses the new hotness, CORS, to do away with the callback function and the other JSONP kludges, and use pure unsullied JSON as Crockford intended.

I’ve updated the code below; the API now goes to version 0.2, so I can still support the JSONP method as well.

(function() {
	var j = '1.9.1', d = document;
	if (window.jQuery === undefined || window.jQuery.fn.jquery < j) {
		var done = false, s = d.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;
				jQuery.noConflict();
				lgfPost();
			}
		};
		d.getElementsByTagName('head')[0].appendChild(s);
	} else {
		lgfPost();
	}
	function esc(s) {
		return escape(s).replace(/\+/ig, '%252B');
	}
	function lgfPost() {
		(window.postPage = function() {
			jQuery.ajax('http://littlegreenfootballs.com/api/0.2/postRegex').done(function(lgf) {
				var f = 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'));
				}
				q = (
					'u=' + u +
					'&t=' + t +
					'&f=' + f +
					'&s=' + s
				);
				w = (
					'width=670,' +
					'height=820,' +
					'scrollbars=1,' +
					'status=1,' +
					'menubar=1,' +
					'location=1,' +
					'resizable=1'
				);
				window.open('http://littlegreenfootballs.com/weblog/lgf-postpage.php?' + q, '_blank', w);
			});
		})();
	}
})();

Jump to top

Create a PageThis is the LGF Pages posting bookmarklet. To use it, drag this button to your browser's bookmark bar, and title it 'LGF Pages' (or whatever you like). Then browse to a site you want to post, select some text on the page to use for a quote, click the bookmarklet, and the Pages posting window will appear with the title, text, and any embedded video or audio files already filled in, ready to go.
Or... you can just click this button to open the Pages posting window right away.
Last updated: 2023-04-04 11:11 am PDT
LGF User's Guide RSS Feeds

Help support Little Green Footballs!

Subscribe now for ad-free access!Register and sign in to a free LGF account before subscribing, and your ad-free access will be automatically enabled.

Donate with
PayPal
Cash.app
Recent PagesClick to refresh
Best of April 2024 Nothing new here but these are a look back at the a few good images from the past month. Despite the weather, I was quite pleased with several of them. These were taken with older lenses (made from the ...
William Lewis
Yesterday
Views: 140 • Comments: 0 • Rating: 4
Texas County at Center of Border Fight Is Overwhelmed by Migrant Deaths EAGLE PASS, Tex. - The undertaker lighted a cigarette and held it between his latex-gloved fingers as he stood over the bloated body bag lying in the bed of his battered pickup truck. The woman had been fished out ...
Cheechako
3 weeks ago
Views: 398 • Comments: 0 • Rating: 1