From 2492d9e61fdc733e2893442e480d47bed5a27285 Mon Sep 17 00:00:00 2001 From: Bart Teeuwisse Date: Sun, 27 Nov 2011 22:43:26 -0800 Subject: [PATCH 1/7] Merging anithri/master. --- README.markdown | 6 ++++++ Rakefile | 10 +++++++++- bin/djsd | 31 +++++++++++++++++++++++++------ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/README.markdown b/README.markdown index 5e619b0..4fcae82 100644 --- a/README.markdown +++ b/README.markdown @@ -17,6 +17,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 `~/.css` and .gif, .jpg, and .png files +from `~/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 @@ -33,6 +38,7 @@ modifications. With dotjs, just add or edit files in ![](https://bit.ly/gAHTbC) + ## How It Works Chrome extensions can't access the local filesystem, diff --git a/Rakefile b/Rakefile index 9026787..3be0adf 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 5d256ab..93e537f 100755 --- a/bin/djsd +++ b/bin/djsd @@ -14,19 +14,38 @@ end require 'webrick' dotjs = Class.new(WEBrick::HTTPServlet::AbstractServlet) do + IMAGE_TYPES = %w(.png .jpg .gif) + def do_GET(request, response) - file = File.expand_path("#{request.path.gsub('/','')}") - default = File.expand_path("default.js") + files = [] + ext = File.extname(request.path) + case + when ext == '.js' + files << File.expand_path("~/.js/default.js") + files << File.expand_path("~/.js/#{request.path.gsub('/','')}") + mime_type = 'text/javascript' + when ext == '.css' + files << File.expand_path("~/.js/css/default.css") + files << File.expand_path("~/.js/css/#{request.path.gsub('/','')}") + mime_type = 'text/css' + when IMAGE_TYPES.include?(ext) + files << File.expand_path("~/.js/images/#{request.path.gsub('/','')}") + mime_type = "image/#{ext[1 .. -1]}" + else + puts "oops" + end - body = "// dotjs is working! //\n" - body << File.read(default) + "\n" if File.file?(default) - body << File.read(file) if File.file?(file) + body = '' + files.each do |f| + body << File.read(f) if File.exists?(f) + body << "\n" + end 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 From 4b4e625439554f696b18f557a451c1668f1458b0 Mon Sep 17 00:00:00 2001 From: Bart Teeuwisse Date: Mon, 5 Dec 2011 23:24:04 -0800 Subject: [PATCH 2/7] - Load .css upon page load. --- ext/dotjs.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ext/dotjs.js b/ext/dotjs.js index 30ae171..c9bc81b 100644 --- a/ext/dotjs.js +++ b/ext/dotjs.js @@ -1,3 +1,13 @@ +$.ajax({ + url: 'http://localhost:3131/'+window.location.host.replace('www.','')+'.css', + dataType: 'text', + success: function(d) { + $('head').prepend('') + }, + error: function(){ + console.log('no dotjs server found at localhost:3131') + } +}); $.ajax({ url: 'http://localhost:3131/'+window.location.host.replace('www.','')+'.js', dataType: 'text', From 272f2b22d925983d9ddc5f4b69479275dbdea924 Mon Sep 17 00:00:00 2001 From: Bart Teeuwisse Date: Mon, 5 Dec 2011 23:29:36 -0800 Subject: [PATCH 3/7] - Updating triple bonus in README.markdown. --- README.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index 4fcae82..9973b72 100644 --- a/README.markdown +++ b/README.markdown @@ -19,8 +19,8 @@ functions in it. Triple bonus: Include CSS and image files in your .js files and serve them from your local dirs. CSS -files in `~/.css` and .gif, .jpg, and .png files -from `~/images`. +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 From 9f755a8dcff55e685e7737784770715d7cb36f34 Mon Sep 17 00:00:00 2001 From: Bart Teeuwisse Date: Wed, 18 Jul 2012 20:31:32 -0700 Subject: [PATCH 4/7] Upgraded to manifest_version 2 --- ext/manifest.json | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/manifest.json b/ext/manifest.json index 4525bc3..f09c16e 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -1,6 +1,7 @@ { "name": "dotjs", "version": "1.5", + "manifest_version": 2, "description": "~/.js", "icons": { "48": "icon48.png", "128": "icon128.png" }, From 1241360b8df96539c7ba5e1927a11ccead337469 Mon Sep 17 00:00:00 2001 From: Bart Teeuwisse Date: Tue, 6 May 2014 12:37:32 -0700 Subject: [PATCH 5/7] Upgrade to manifest version 2. --- ext/manifest.json | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/ext/manifest.json b/ext/manifest.json index f09c16e..5aca438 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -3,14 +3,18 @@ "version": "1.5", "manifest_version": 2, "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" ] From 69de85f8bcafdaadb0a105bd6191f47e5c40bb6f Mon Sep 17 00:00:00 2001 From: Bart Teeuwisse Date: Tue, 6 May 2014 12:38:38 -0700 Subject: [PATCH 6/7] Adding .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2f516a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.crx +*.pem From a9371523381c5edb526ad98f4520fdabcc4f4f84 Mon Sep 17 00:00:00 2001 From: Bart Teeuwisse Date: Thu, 11 Dec 2014 15:02:45 -0800 Subject: [PATCH 7/7] Make it work for CSS & images Use 127.0.0.1 because localhost mapped to an IPV6 address in /etc/hosts --- bin/djsd | 21 +++++++++++---------- ext/dotjs.js | 4 ++-- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/bin/djsd b/bin/djsd index 2c35c2e..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 @@ -18,7 +18,7 @@ dotjs = Class.new(WEBrick::HTTPServlet::AbstractServlet) do IMAGE_TYPES = %w(.png .jpg .gif) def do_GET(request, response) - (body, mime_type) = build_body(request.path) + (body, mime_type) = build_body(request.path) response.status = body.empty? ? 204 : 200 if origin = detect_origin(request) @@ -30,7 +30,7 @@ dotjs = Class.new(WEBrick::HTTPServlet::AbstractServlet) do def build_body(path) files = [] - ext = File.extname(request.path) + ext = File.extname(path) base = '~/.js/' case when ext == '.js' @@ -44,17 +44,17 @@ dotjs = Class.new(WEBrick::HTTPServlet::AbstractServlet) do base += 'images/' mime_type = "image/#{ext[1 .. -1]}" else - puts "oops" + puts 'oops' end paths = path.gsub('/','').split('.') until paths.empty? - file = File.expand_path([base].concat(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) @@ -79,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 63ef7ac..9120f3c 100644 --- a/ext/dotjs.js +++ b/ext/dotjs.js @@ -1,5 +1,5 @@ $.ajax({ - url: 'https://localhost:3131/'+location.hostname.replace(/www\./,'')+'.css', + url: 'https://127.0.0.1:3131/'+location.hostname.replace(/www\./,'')+'.css', dataType: 'text', success: function(d) { $('head').prepend('') @@ -9,7 +9,7 @@ $.ajax({ } }); $.ajax({ - url: 'https://localhost:3131/'+location.hostname.replace(/^www\./,'')+'.js', + url: 'https://127.0.0.1:3131/'+location.hostname.replace(/^www\./,'')+'.js', dataType: 'text', success: function(d){ $(function(){ eval(d) })