Skip to content

Instantly share code, notes, and snippets.

@satyr
Created March 11, 2010 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satyr/329110 to your computer and use it in GitHub Desktop.
Save satyr/329110 to your computer and use it in GitHub Desktop.
linkopen (fork)
CmdUtils.CreateCommand({
names: ['open links', 'linkopen'],
description: 'Opens links from among the current page.',
icon: 'chrome://ubiquity/skin/icons/tab_go.png',
arguments: [{role: 'object', nountype: noun_arb_text}],
preview: function preview(pblock, {object: {text}}) {
var me = this, links = me._links(text);
if(!links.length) return void this.previewDefault(pblock);
CmdUtils.previewList(
pblock, [href.link(href) for each({href} in links)],
function(i)(me._go(links[i]), true));
},
execute: function preview({object: {text}}) {
if(!text) return displayMessage('please enter keyword.', this);
this._links(text).forEach(this._go);
},
_links: function lo_links(text) {
var links = this._dive([], CmdUtils.window);
if(text){
let re = Utils.regexp(text, 'i');
links = [l for each(l in links) if(re.test(l.href))];
}
return Utils.uniq(links, 'href');
},
_dive: function lo_dive(lms, win) {
var all = win.document.querySelectorAll('a[href], link[href]');
lms.push.apply(lms, Array.slice(all));
return Array.reduce(win, lo_dive, lms);
},
_go: function lo_go(link) {
if(link.protocol === 'javascript:' || link.getAttribute('href')[0] === '#')
link.ownerDocument.location = link.href;
else
Utils.openUrlInBrowser(link.href);
},
author: {name: 'powchin', homepage: 'http://friendfeed.com/powchin'},
contributor: 'satyr',
});
CmdUtils.CreateCommand({
names: ['open selections', 'selopen'],
execute: function so_execute(){
var {text, html} = CmdUtils.selectionObject;
Utils.openUrlInBrowser(
'data:text/html;charset=utf-8,'+
encodeURI('<title>'+ Utils.escapeHtml(text) +'</title>'+ html));
},
preview: function so_preview(pb){
pb.innerHTML =
'<pre id="selopen">'+ Utils.escapeHtml(CmdUtils.htmlSelection);
},
author: 'satyr',
});
feed.license = 'MIT';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment