diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2f516a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.crx +*.pem diff --git a/README.markdown b/README.markdown index be1fc26..f63bef1 100644 --- a/README.markdown +++ b/README.markdown @@ -21,6 +21,11 @@ Double bonus: `~/.js/default.js` is loaded on every request, meaning you can stick plugins or helper functions in it. +Triple bonus: Include CSS and image files in your +.js files and serve them from your local dirs. CSS +files in `~/.js/css` and .gif, .jpg, and .png files +from `~/.js/images`. + GreaseMonkey user scripts are great, but you need to publish them somewhere and re-publish after making modifications. With dotjs, just add or edit files in @@ -38,6 +43,7 @@ modifications. With dotjs, just add or edit files in ![](http://puu.sh/1Kjvw) + ## How It Works Chrome extensions can't access the local filesystem, diff --git a/Rakefile b/Rakefile index 31dfa10..17eb908 100644 --- a/Rakefile +++ b/Rakefile @@ -61,12 +61,20 @@ namespace :install do cp "bin/djsd", DAEMON_INSTALL_DIR, :verbose => true, :preserve => true end - desc "Create ~/.js" + desc "Create ~/.js/{css,images}" task :create_dir do if !File.directory? js_dir = File.join(ENV['HOME'], ".js") mkdir js_dir chmod 0755, js_dir end + if !File.directory? css_dir = File.join(ENV['HOME'], ".js", "css") + mkdir css_dir + chmod 0755, css_dir + end + if !File.directory? images_dir = File.join(ENV['HOME'], ".js", "images") + mkdir images_dir + chmod 0755, images_dir + end end desc "Install Google Chrome extension" diff --git a/bin/djsd b/bin/djsd index 43cf413..7728962 100755 --- a/bin/djsd +++ b/bin/djsd @@ -1,13 +1,13 @@ #!/usr/bin/env ruby if (%w( -h --help -help help ) & ARGV).length > 0 - puts "usage: djsd [-hv]" - puts "starts dotjs server in the foreground. kill with ^C" + puts 'usage: djsd [-hv]' + puts 'starts dotjs server in the foreground. kill with ^C' exit end if ARGV.include?('-v') - puts "djsd 2.0" + puts 'djsd 2.0' exit end @@ -15,34 +15,52 @@ require 'webrick' require 'webrick/https' dotjs = Class.new(WEBrick::HTTPServlet::AbstractServlet) do + IMAGE_TYPES = %w(.png .jpg .gif) + def do_GET(request, response) - body = build_body(request.path) + (body, mime_type) = build_body(request.path) response.status = body.empty? ? 204 : 200 if origin = detect_origin(request) response['Access-Control-Allow-Origin'] = origin end - response['Content-Type'] = 'text/javascript' + response['Content-Type'] = mime_type response.body = body end def build_body(path) - files = [File.expand_path("default.js")] + files = [] + ext = File.extname(path) + base = '~/.js/' + case + when ext == '.js' + files << File.expand_path('~/.js/default.js') + mime_type = 'text/javascript' + when ext == '.css' + files << File.expand_path('~/.js/css/default.css') + base += 'css/' + mime_type = 'text/css' + when IMAGE_TYPES.include?(ext) + base += 'images/' + mime_type = "image/#{ext[1 .. -1]}" + else + puts 'oops' + end paths = path.gsub('/','').split('.') until paths.empty? - file = File.expand_path(paths.join('.')) + file = File.expand_path(File.join(base, (paths).join('.'))) files << file if File.file?(file) paths.shift end - body = "// dotjs is working! //\n" + body = '' files.each do |file| body << File.read(file) + "\n" if File.file?(file) end - body + [body, mime_type] end def detect_origin(req) @@ -61,14 +79,15 @@ ssl_cert = ssl_info.scan(/(-----BEGIN CERTIFICATE-----.+?-----END CERTIFICATE--- ssl_key = ssl_info.scan(/(-----BEGIN RSA PRIVATE KEY-----.+?-----END RSA PRIVATE KEY-----)/m)[0][0] server_options = { - :BindAddress => "127.0.0.1", + :BindAddress => '127.0.0.1', :Port => 3131, :AccessLog => [], :SSLEnable => true, :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE, :SSLPrivateKey => OpenSSL::PKey::RSA.new(ssl_key), :SSLCertificate => OpenSSL::X509::Certificate.new(ssl_cert), - :SSLCertName => [["CN", WEBrick::Utils::getservername]], + :SSLCertName => [['CN', WEBrick::Utils::getservername]], + :SSLCACertificatePath => File.join(__FILE__, '../dotjs.pem') } server = WEBrick::HTTPServer.new(server_options) diff --git a/ext/dotjs.js b/ext/dotjs.js index 2361a5e..9120f3c 100644 --- a/ext/dotjs.js +++ b/ext/dotjs.js @@ -1,5 +1,15 @@ $.ajax({ - url: 'https://localhost:3131/'+location.hostname.replace(/^www\./,'')+'.js', + url: 'https://127.0.0.1:3131/'+location.hostname.replace(/www\./,'')+'.css', + dataType: 'text', + success: function(d) { + $('head').prepend('') + }, + error: function(){ + console.log('no dotjs server found at localhost:3131') + } +}); +$.ajax({ + url: 'https://127.0.0.1:3131/'+location.hostname.replace(/^www\./,'')+'.js', dataType: 'text', success: function(d){ $(function(){ eval(d) }) diff --git a/ext/manifest.json b/ext/manifest.json index 1e1b474..39093dd 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -3,14 +3,18 @@ "manifest_version": 2, "version": "2.0", "description": "~/.js", - "icons": { "48": "icon48.png", - "128": "icon128.png" }, - "content_scripts": [{ - "all_frames": true, - "run_at": "document_start", - "matches": ["http://*/*", "https://*/*"], - "js": ["jquery.js", "dotjs.js"] - }], + "icons": { + "48": "icon48.png", + "128": "icon128.png" + }, + "content_scripts": [ + { + "all_frames": true, + "run_at": "document_start", + "matches": ["http://*/*", "https://*/*"], + "js": ["jquery.js", "dotjs.js"] + } + ], "permissions": [ "tabs" ]