Skip to content

Instantly share code, notes, and snippets.

@Geobert
Created April 10, 2009 20:54
Show Gist options
  • Save Geobert/93288 to your computer and use it in GitHub Desktop.
Save Geobert/93288 to your computer and use it in GitHub Desktop.
CmdUtils.CreateCommand({
name: "open-link",
_getSelectedLinks: function() {
var doc = CmdUtils.getDocument();
var links = doc.links;
if (null == links)
return {urls: null, nbLinks: 0};
var nbLinks = links.length;
var urlhash = new Object();
var selection = CmdUtils.getWindow().getSelection();
var numOfSel = 0;
for (var i = 0; i < nbLinks; i++) {
var link = links[i].href;
if (selection.containsNode(links[i], true)) {
numOfSel++;
urlhash[link] = true;
}
}
return {urls: urlhash, nbLinks: numOfSel};
},
_isOpenable: function(link) {
try {
var authorized = new Object();
authorized['http'] = true;
authorized['https'] = true;
authorized['file'] = true;
var endOfProtocolIdx = link.indexOf(':');
var protocol = link.substr(0, endOfProtocolIdx);
CmdUtils.log('protocol is : ' + protocol);
return authorized[protocol.toLowerCase()] == true;
} catch(e) {
CmdUtils.log("Unable to get protocol from " + link);
return false;
}
},
_openLink: function(link) {
var browser = Utils.currentChromeWindow.getBrowser();
browser.addTab(link);
},
preview: function(pblock) {
var msg = '';
var infos = this._getSelectedLinks();
if (infos.nbLinks == 0) {
msg = 'No link in selected text.';
} else if (infos.nbLinks > 20) {
msg = 'Can\'t open ${nbLinks} at the same time (20 max).';
} else if (infos.nbLinks > 0) {
msg = 'Open ${nbLinks} links in tabs.';
} else {
msg = 'Error.';
}
var nbAuthorized = 0;
for (link in infos.urls) {
if (this._isOpenable(link))
nbAuthorized++;
}
pblock.innerHTML = CmdUtils.renderTemplate(msg, {nbLinks: nbAuthorized});
},
execute: function() {
var infos = this._getSelectedLinks();
var links = infos.urls;
if (infos.nbLinks > 0 && infos.nbLinks <= 20) {
for (link in links) {
if (this._isOpenable(link)) {
this._openLink(link);
}
}
}
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment