Advertisement
Guest User

markdown list of chrome tabs

a guest
Feb 10th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.92 KB | None | 0 0
  1. #!/usr/bin/env ruby -rjcode -Ku
  2.  
  3. # grabs Chrome tabs
  4. # creates markdown reference list
  5. # generates unique, short titles based on domains
  6. # truncates feedburner stuff
  7.  
  8. $KCODE = 'u'
  9. UNICODE_COMPETENT = ((RUBY_VERSION)[0..2].to_f > 1.8)
  10.  
  11. unless UNICODE_COMPETENT # lower than 1.9
  12.   class String
  13.      def utf8_length
  14.         i = 0
  15.         i = self.scan(/(.)/).length
  16.         i
  17.      end
  18.   end
  19. else # 1.9 and above
  20.   class String
  21.      alias_method :utf8_length, :length
  22.   end
  23. end
  24.  
  25. def chrome_refs
  26.   input = %x{osascript -e 'tell application \"Google Chrome\"' -e 'set tabList to every tab of window 1' -e 'set urlList to \"\"' -e 'repeat with aTab in tabList' -e 'set aLink to URL of aTab & ASCII character 10' -e 'set urlList to urlList & aLink' -e 'end repeat' -e 'return urlList' -e 'end tell'}.chomp
  27.   links = input.scan /(?:\[([^\]]+)\]\: )?(https?:\/\/[^ \n"]+)/m
  28.  
  29.  if links.empty? && input.empty?
  30.     exit
  31.  else
  32.    norepeat = []
  33.    output = []
  34.    links.each {|url|
  35.      url[1] = url[1].sub(/[\?&]utm_source=feed.*$/,'')
  36.         if url[0].nil?
  37.           if url[1] =~ (/http:\/\/itunes.apple.com\/(.+?)\/app\/(.+?)\/id\d+/)
  38.             name = $2
  39.           elsif url[1] =~ /(itunes|phobos).apple.com/
  40.             name = "itunes"
  41.           else
  42.               domain = url[1].match(/https?:\/\/([^\/]+)/)
  43.               parts = domain[1].split('.')
  44.               name = case parts.length
  45.                   when 1: parts[0]
  46.                   when 2: parts[0]
  47.                   else parts[1]
  48.               end
  49.             end
  50.         else
  51.             name = url[0]
  52.         end
  53.  
  54.         while norepeat.include? name
  55.             if name =~ / ?[0-9]$/
  56.                 name.next!
  57.             else
  58.                 name = name + " 2"
  59.             end
  60.         end
  61.         output << {'title' => name, 'link' => url[1] }
  62.         norepeat.push name
  63.    }
  64.    output = output.sort {|a,b| a['title'] <=> b['title']}
  65.    counter = 0
  66.    output.each { |x|
  67.         counter += 1
  68.         puts "[#{x['title']}]: #{x['link']}"
  69.     }
  70.   end
  71. end
  72.  
  73. chrome_refs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement