Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created September 4, 2015 18:44
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ttscoff/45d154d49c301f0d3bb0 to your computer and use it in GitHub Desktop.
Save ttscoff/45d154d49c301f0d3bb0 to your computer and use it in GitHub Desktop.
Browser tabs to Dropbox HTML list
# Share Tab Collection 1.0 | Brett Terpstra 2015 <http://brettterpstra.com>
USERID = 'XXXXX'
require 'shellwords'
require 'fileutils'
def class_exists?(class_name)
klass = Module.const_get(class_name)
return klass.is_a?(Class)
rescue NameError
return false
end
if class_exists? 'Encoding'
Encoding.default_external = Encoding::UTF_8 if Encoding.respond_to?('default_external')
Encoding.default_internal = Encoding::UTF_8 if Encoding.respond_to?('default_internal')
end
if USERID == 'XXXXX'
$stderr.puts "Dropbox USERID not set"
%x{osascript -e 'display notification "Please open Share Tab Collection.workflow in Automator and set the USERID Dropbox variable"'}
Process.exit 1
end
begin
title = ARGV.join(' ').force_encoding('utf-8')
rescue
title = ARGV.join(' ')
end
if title.strip == ''
title = "Link Collection #{Time.now.strftime('%m-%d-%y at %-I:%M%P')}"
else
title += %Q{ <small class="date">#{Time.now.strftime('%m-%d-%y at %-I:%M%P')}</small>}
end
def chrome_tabs
links = %x{osascript <<-APPLESCRIPT
tell application "System Events"
set chromeRunning to (count of (every process whose bundle identifier is "com.google.Chrome")) > 0
set safariRunning to (count of (every process whose bundle identifier is "com.apple.Safari")) > 0
end tell
if chromeRunning then
tell application "Google Chrome"
set urlList to {}
set tabList to every tab of window 1
repeat with aTab in tabList
set end of urlList to (title of aTab) & ":!:" & (URL of aTab) & (ASCII character 10)
end repeat
return urlList as string
end tell
else if safariRunning then
tell application "Safari"
set urlList to {}
set tabList to every tab of window 1
repeat with aTab in tabList
set end of urlList to (name of aTab) & ":!:" & (URL of aTab) & (ASCII character 10)
end repeat
return urlList as string
end tell
end if
APPLESCRIPT}.strip.split(/\n/)
output = []
if links.empty?
return nil
else
links.each {|line|
title, url = line.split(/:!:/)
if title && url
url.gsub!(/[\?&]utm_.*?(?=&)/,'')
if url =~ /&/ && url !~ /\?/
url.sub!(/&/,'?')
end
output << {:title => title, :url => url}
end
}
end
output
end
def url_escape(text)
return text.gsub(/([^a-zA-Z0-9_.-]+)/n) {
'%' + $1.unpack('H2' * $1.size).join('%').upcase
}
end
links = chrome_tabs
htmlhead =<<-HTMLHEAD
<!DOCTYPE html>
<html>
<head>
<title>Link Collection</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600,700&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
<style type="text/css" media="screen">
html>body {
font-size: 16px;
}
body {
-webkit-font-smoothing: antialiased;
font: normal .8764em/1.5em Arial, Verdana, sans-serif;
margin: 0
}
li {
font-size: 100%;
list-style-type: circle;
}
li li {
font-size: 100%
}
li p {
font-size: 100%;
margin: .5em 0
}
ul, ol {
list-style-position: inside;
width: 100%;
padding: 0;
}
h1 {
color: #444;
font-size: 1.4em;
line-height: 1.6563em;
margin: 1em 0;
border-bottom: dashed 2px #ccc;
font-weight: 300;
}
h2 {
color: #111;
font-size: 1.7143em;
line-height: .875em;
margin: .875em 0
}
h3 {
color: #111;
font-size: 1.5em;
line-height: 1em;
margin: 1em 0
}
h4 {
color: #111;
font-size: 1.2857em;
line-height: 1.1667em;
margin: 1.1667em 0
}
h5 {
color: #111;
font-size: 1.15em;
line-height: 1.3em;
margin: 1.3em 0
}
h6 {
font-size: 1em;
line-height: 1.5em;
margin: 1.5em 0
}
body, p, td, div {
color: #111;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, Verdana, sans-serif;
word-wrap: break-word
}
#wrapper {
background: #fff;
color: #303030;
font-size: 100%;
position: relative;
text-indent: 0;
max-width: 600px;
margin: 50px auto;
padding: 20px;
}
::selection {
background: rgba(157, 193, 200, .5)
}
p {
font-weight: 300;
font-size: 15px;
}
#openbunchlink {
border-top: dashed 2px #ccc;
padding-top: 10px;
}
#openbunchlink a {
display: block;
color: rgb(36, 150, 179);
text-align: center;
font-weight: bold;
}
#openbunchlink a:hover {
color: rgb(0, 176, 220);
}
a {
text-decoration: none;
-webkit-transition: all .2s ease-in-out;
-moz-transition: all .2s ease-in-out;
-o-transition: all .2s ease-in-out;
transition: all .1s ease-in;
font-weight: 600;
display: block;
margin: 4px 0;
padding: 1px 6px;
color: #3c8599;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
border: dotted 1px transparent;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
a:hover {
border-color: rgba(113, 195, 216, .6);
background-color: rgba(182, 240, 255, .2);
}
small.date {
font-size: 13px;
color: #777;
float: right;
}
</style>
</head>
<body>
<div id="wrapper">
<h1>#{title}</h1>
<ol id="links">
HTMLHEAD
unless links.empty?
o = ""
links.each { |link|
o += "<li><a href=\"#{link[:url]}\">#{link[:title]}</a></li>\n"
}
htmlhead += o
htmlhead +=<<-HTMLFOOTER
</ol>
<p id="openbunchlink">
<a onclick="if (confirm('Open ALL links? (Be sure popups are allowed in your browser)')) { openBunch(); }" href="javascript:void();">Open All</a>
</p>
</div>
<script type="text/javascript">
function openBunch() {
var bunch = document.getElementById('links').getElementsByTagName('a'); var i; var l;
for (i=0; l = bunch[i]; i++) {
open(l.href);
}
}
</script>
</body>
</html>
HTMLFOOTER
datename = Time.now.strftime('%m-%d-%y_%I%M')
db_folder = File.expand_path("~/Dropbox/Public/LinkCollections/")
unless File.directory?(db_folder)
if File.exists?(db_folder)
print "Public/LinkCollections exists and is not a folder"
Process.exit
else
FileUtils.mkdir_p(db_folder)
end
end
target = File.join(db_folder,"#{datename}.html")
fp = File.open(target,'w')
fp.puts htmlhead
fp.close
public_link = "http://dl.dropbox.com/u/#{USERID}/LinkCollections/#{datename}.html"
# %x{/bin/echo -n #{Shellwords.escape(public_link)}|pbcopy}
print public_link
else
print "Chrome returned empty tab set"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment