Skip to content

Instantly share code, notes, and snippets.

@satyr
Created January 8, 2009 21:17
Show Gist options
  • Save satyr/44888 to your computer and use it in GitHub Desktop.
Save satyr/44888 to your computer and use it in GitHub Desktop.
Converts image into dataURL.
CmdUtils.CreateCommand({
name: "image2dataurl",
synonyms: ['i2d'],
icon: 'chrome://ubiquity/skin/icons/convert.png',
description: 'Converts image into dataURL.',
takes: {'image URL': {_name: '_', suggest: function(txt, htm, cb, sx){
return sx ? [] : [CmdUtils.makeSugg(txt)];
}}},
execute: function(inp){
this._withDataURL(inp, function(du){
CmdUtils.copyToClipboard(du);
CmdUtils.setLastResult(du);
displayMessage({
icon: this.icon, title: this.name, text: 'Copied to clipboard.'});
});
},
preview: function(pbl, inp){
this._withDataURL(inp, function(du){
pbl.innerHTML = <div class={this.name} style="text-align:center"><pre
style="overflow:hidden"><a href={du}
accesskey="d">{du}</a></pre><img src={du}/></div>;
}, function(){ pbl.innerHTML = this.description });
},
previewDelay: 17,
_withDataURL: function(inp, ok, ng){
var me = this, img = inp.text || this._getImageURL();
img ? CmdUtils.getImageSnapshot(img, function(du) ok.call(me, du))
: ng && ng.call(this);
},
_getImageURL: function(){
return (jQuery('img', CmdUtils.getHtmlSelection()).attr('src') ||
this._fromCSS() || CmdUtils.getWindow().location.href);
},
_fromCSS: function(){ try{
var win = CmdUtils.getWindowInsecure(), sel = win.getSelection(),
stl = win.getComputedStyle(
sel.isCollapsed ? win.document.activeElement : sel.anchorNode, '');
return (stl.backgroundImage || stl.listStyleImage)
.replace(/^url\(|\)$|^none$/g, '');
} catch(e){}},
author: 'satyr'.link('http://d.hatena.ne.jp/murky-satyr'), license: 'MIT',
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment