Skip to content

Instantly share code, notes, and snippets.

@swdyh
Created December 15, 2008 03:28
Show Gist options
  • Save swdyh/35857 to your computer and use it in GitHub Desktop.
Save swdyh/35857 to your computer and use it in GitHub Desktop.
// Tabs - jsactions script
(function(){
var html_dir = '/Users/youhei/Desktop/tabs/'
main()
function main() {
var dim = {h: window.innerHeight, w: window.innerWidth}
var list = Array.map(gBrowser.mTabs, function(tab) {
var w = gBrowser.getBrowserForTab(tab).contentWindow
var c = capture(w, {x: 0, y:0}, dim, 0.3)
return [w.document.title, w.location.href, c]
})
var size = gBrowser.mTabs.length.toString()
var date = dateString()
var path = html_dir + date + '_' + size + '.html'
var title = size + 'Tabs ' + date
var html = []
html.push(['<html>'])
html.push(['<head>'])
html.push(['<title>', title, '</title>'])
html.push(['<style type="text/css">', style(), '</style>'])
html.push(['</head>'])
html.push(['<body>'])
html.push(['<h1>', title, '</h1>'])
html.push(['<ul>'])
list.forEach(function(i) {
html.push(['<li>'])
html.push(['<div class="capture"><a href="', i[1], '">',
'<img style="max-width:300px;" src="', i[2],
'"/></a></div>'])
html.push(['<div class="title"><a href="', i[1], '">', i[0], '</a></div>'])
html.push(['<div class="url"><a href="', i[1], '">', i[1], '</a></div>'])
html.push(['<br />'])
html.push(['</li>'])
})
html.push(['</ul>'])
html.push(['</body>'])
html.push(['</html>'])
write(path, render(html))
gBrowser.selectedTab = gBrowser.addTab('file://' + path)
}
function render(html) {
return html.map(function(i) { return i.join('') }).join('\n')
}
function dateString() {
var d = new Date()
return [d.getFullYear(), d.getMonth() + 1,
d.getDate()].map(function(i) { return i.toString() }).join('-')
}
function style() {
return <><![CDATA[
body {
margin: 0;
}
h1 {
background-color: #0af;
color: #fff;
padding: 10px;
border-bottom: 3px solid #ddd;
}
a {
color: #0af;
}
a img {
border: 1px solid #ccc;
}
ul {
list-style: none;
}
.title a {
text-decoration: none;
}
.capture {
float:left;
margin: 0 50px 70px 0;
}
.url {
font-size: 80%;
text-decoration: none;
}
li > br {
clear: both;
}
]]></>.toString()
}
function log() {
Firebug.Console.log(arguments)
}
function write(path, data, opt) {
const LocalFile = Components.Constructor('@mozilla.org/file/local;1', 'nsILocalFile', 'initWithPath');
var writePath = new LocalFile(path);
var outStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
outStream.init(writePath, 0x04 | 0x08 | 0x20, 0664, 0); // write, create, truncate
// outStream.write(data, data.length);
// outStream.close
var opt = opt || {}
var charset = opt.charset || "UTF-8";
var os = Components.classes["@mozilla.org/intl/converter-output-stream;1"]
.createInstance(Components.interfaces.nsIConverterOutputStream);
os.init(outStream, charset, 0, 0x0000);
os.writeString(data)
os.close();
}
// [email protected]/chrome/content/library/01_utility.js
function capture(win, pos, dim, scale){
var HTML_NS = 'http://www.w3.org/1999/xhtml';
var canvas = document.createElementNS(HTML_NS, 'canvas');
var ctx = canvas.getContext('2d');
canvas.width = dim.w;
canvas.height = dim.h;
if(scale){
scale = scale.w? scale.w/dim.w :
scale.h? scale.h/dim.h : scale;
canvas.width = dim.w * scale;
canvas.height = dim.h * scale;
ctx.scale(scale, scale);
}
ctx.drawWindow(win, pos.x, pos.y, dim.w, dim.h, '#FFF');
return canvas.toDataURL('image/png', '');
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment