-
-
Save mralexgray/1430622 to your computer and use it in GitHub Desktop.
A small Macruby script to display HTML piped in from STDIN.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
framework 'Cocoa' | |
framework 'WebKit' | |
require 'optparse' | |
class Browser | |
attr_accessor :view | |
def initialize | |
@view = WebView.alloc.initWithFrame([0, 0, 520, 520]) | |
@window = NSWindow.alloc.initWithContentRect([200, 200, 520, 520], | |
styleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask, | |
backing:NSBackingStoreBuffered, | |
defer:false) | |
@window.contentView = view | |
# Use the screen stylesheet, rather than the print one. | |
view.mediaStyle = 'screen' | |
view.customUserAgent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; en-us) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10' | |
# Make sure we don't save any of the prefs that we change. | |
view.preferences.autosaves = false | |
# Set some useful options. | |
view.preferences.shouldPrintBackgrounds = true | |
view.preferences.javaScriptCanOpenWindowsAutomatically = false | |
# view.preferences.allowsAnimatedImages = false | |
view.preferences.plugInsEnabled = false | |
view.mainFrame.frameView.allowsScrolling = true | |
view.frameLoadDelegate = self | |
end | |
def show(url, string) | |
view.mainFrame.loadHTMLString(string, baseURL: (url.kind_of?(NSURL) ? url : NSURL.URLWithString(url.to_s))) | |
end | |
def fetch(url) | |
page_url = NSURL.URLWithString(url) | |
view.mainFrame.loadRequest(NSURLRequest.requestWithURL(page_url)) | |
end | |
# Delegate methods | |
def webView(view, didFinishLoadForFrame: frame) | |
@window.display | |
@window.orderFrontRegardless | |
end | |
end | |
url = nil | |
opts = OptionParser.new | |
opts.on("-u", "--url URL", String) {|val| url = val} | |
opts.parse(*ARGV) | |
NSApplication.sharedApplication | |
browser = Browser.new | |
if STDIN.tty? | |
browser.fetch(url) | |
else | |
browser.show(url, STDIN.read) | |
end | |
NSRunLoop.currentRunLoop.runUntilDate(NSDate.distantFuture) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment