postprefix
4/26/2011 - 1:50 AM

A smarter bookmarklet for Pinboard.in; wraps selected text in <blockquote> tags, keeps variables out of the global scope, supports filling t

A smarter bookmarklet for Pinboard.in; wraps selected text in

tags, keeps variables out of the global scope, supports filling tags from rel="tag" markup in the page, and titles from hAtom entry titles.

javascript:(function()%20{var%20silent%20=%20false;var%20url%20=%20location.href;var%20title%20=%20document.title;if(document.querySelector)%20{if(1%20===%20document.getElementsByClassName(%27hentry%27).length)%20{title%20=%20document.querySelector(%27.hentry%20.entry-title,%20.hentry%20h1,%20.hentry%20h2,%20.hentry%20h3%27).innerText;}}if(%27%27%20!==%20(text%20=%20String(document.getSelection())))%20{text%20=%20[%27<blockquote>%27,%20text,%20%27</blockquote>%27,%20%22\n\n%22].join(%27%27);}if(document.querySelectorAll)%20{var%20tags%20=%20[].slice.call(document.querySelectorAll(%27a[rel~=tag]%27),0).map(function(a)%20{return%20a.href.split(%27/%27).pop();}).join(%27%20%27);}var%20args%20=%20[%27http://pinboard.in/add?url=%27,%20encodeURIComponent(url),%27&description=%27,%20encodeURIComponent(text),%27&tags%27,%20encodeURIComponent(tags),%27&title=%27,%20encodeURIComponent(title)];if(silent)%20{args%20=%20args.concat([%27&later=%27,%20%27yes%27,%27&jump=%27,%20%27close%27]);}var%20pin%20=%20open(args.join(%27%27),%20%27Pinboard%27,%20%27toolbar=no,width=610,height=350%27);if(silent)%20{pin.blur();}})();
javascript:(function()%20{var%20silent%20=%20true;var%20url%20=%20location.href;var%20title%20=%20document.title;if(document.querySelector)%20{if(1%20===%20document.getElementsByClassName(%27hentry%27).length)%20{title%20=%20document.querySelector(%27.hentry%20.entry-title,%20.hentry%20h1,%20.hentry%20h2,%20.hentry%20h3%27).innerText;}}if(%27%27%20!==%20(text%20=%20String(document.getSelection())))%20{text%20=%20[%27<blockquote>%27,%20text,%20%27</blockquote>%27,%20%22\n\n%22].join(%27%27);}if(document.querySelectorAll)%20{var%20tags%20=%20[].slice.call(document.querySelectorAll(%27a[rel~=tag]%27),0).map(function(a)%20{return%20a.href.split(%27/%27).pop();}).join(%27%20%27);}var%20args%20=%20[%27http://pinboard.in/add?url=%27,%20encodeURIComponent(url),%27&description=%27,%20encodeURIComponent(text),%27&tags%27,%20encodeURIComponent(tags),%27&title=%27,%20encodeURIComponent(title)];if(silent)%20{args%20=%20args.concat([%27&later=%27,%20%27yes%27,%27&jump=%27,%20%27close%27]);}var%20pin%20=%20open(args.join(%27%27),%20%27Pinboard%27,%20%27toolbar=no,width=610,height=350%27);if(silent)%20{pin.blur();}})();
(function() {
  // Change `silent` to true to invoke the promptless, self-closing 
  // version of the bookmarklet.
  var silent = false;
  
  var url = location.href;
  var title = document.title;

  // Look for a single hAtom entry on the page, and iff one is found, extract
  // the entry-title in place of the document title:
  if(document.querySelector) {
    if(1 === document.getElementsByClassName('hentry').length) {
      title = document.querySelector(
          '.hentry .entry-title, .hentry h1, .hentry h2, .hentry h3'
        ).innerText;
    }
  }
  
  // Grab the text selection (if any) and <blockquote> it in the description.
  if('' !== (text = String(document.getSelection()))) {
    text = ['<blockquote>', text, '</blockquote>', "\n\n"].join('');
  }
  
  // Grab all rel-tag microformats from the page and prefix the tags param
  if(document.querySelectorAll) {
    var tags = [].slice.call(
      document.querySelectorAll('a[rel~=tag]'),0).map(
        function(a) { 
          return a.href.split('/').pop(); 
        }
      ).join(' ');
  }
  
  // Assembles default form pre-fill arguments.
  // Note: 'tags' is not currently supported but should be.
  var args = [
    'http://pinboard.in/add?url=', encodeURIComponent(url),
    '&description=', encodeURIComponent(text),
    '&tags', encodeURIComponent(tags),
    '&title=', encodeURIComponent(title)
  ];
  
  // If silent mode, add the auto-close parameter and read-later flag:
  if(silent) {
    args = args.concat([
      '&later=', 'yes',
      '&jump=', 'close'
    ]);
  }

  var pin = open(args.join(''), 'Pinboard', 'toolbar=no,width=610,height=350');
  
  // Send the window to the background if silent mode.
  if(silent) {
    pin.blur();
  }
  
})();