Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also serve CSS & images, auto-load CSS #56

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*.crx
*.pem
6 changes: 6 additions & 0 deletions README.markdown
Expand Up @@ -21,6 +21,11 @@ Double bonus: `~/.js/default.js` is loaded on every
request, meaning you can stick plugins or helper request, meaning you can stick plugins or helper
functions in it. 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 GreaseMonkey user scripts are great, but you need to
publish them somewhere and re-publish after making publish them somewhere and re-publish after making
modifications. With dotjs, just add or edit files in modifications. With dotjs, just add or edit files in
Expand All @@ -38,6 +43,7 @@ modifications. With dotjs, just add or edit files in


![](http://puu.sh/1Kjvw) ![](http://puu.sh/1Kjvw)



## How It Works ## How It Works


Chrome extensions can't access the local filesystem, Chrome extensions can't access the local filesystem,
Expand Down
10 changes: 9 additions & 1 deletion Rakefile
Expand Up @@ -61,12 +61,20 @@ namespace :install do
cp "bin/djsd", DAEMON_INSTALL_DIR, :verbose => true, :preserve => true cp "bin/djsd", DAEMON_INSTALL_DIR, :verbose => true, :preserve => true
end end


desc "Create ~/.js" desc "Create ~/.js/{css,images}"
task :create_dir do task :create_dir do
if !File.directory? js_dir = File.join(ENV['HOME'], ".js") if !File.directory? js_dir = File.join(ENV['HOME'], ".js")
mkdir js_dir mkdir js_dir
chmod 0755, js_dir chmod 0755, js_dir
end 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 end


desc "Install Google Chrome extension" desc "Install Google Chrome extension"
Expand Down
41 changes: 30 additions & 11 deletions bin/djsd
@@ -1,48 +1,66 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby


if (%w( -h --help -help help ) & ARGV).length > 0 if (%w( -h --help -help help ) & ARGV).length > 0
puts "usage: djsd [-hv]" puts 'usage: djsd [-hv]'
puts "starts dotjs server in the foreground. kill with ^C" puts 'starts dotjs server in the foreground. kill with ^C'
exit exit
end end


if ARGV.include?('-v') if ARGV.include?('-v')
puts "djsd 2.0" puts 'djsd 2.0'
exit exit
end end


require 'webrick' require 'webrick'
require 'webrick/https' require 'webrick/https'


dotjs = Class.new(WEBrick::HTTPServlet::AbstractServlet) do dotjs = Class.new(WEBrick::HTTPServlet::AbstractServlet) do
IMAGE_TYPES = %w(.png .jpg .gif)

def do_GET(request, response) def do_GET(request, response)
body = build_body(request.path) (body, mime_type) = build_body(request.path)


response.status = body.empty? ? 204 : 200 response.status = body.empty? ? 204 : 200
if origin = detect_origin(request) if origin = detect_origin(request)
response['Access-Control-Allow-Origin'] = origin response['Access-Control-Allow-Origin'] = origin
end end
response['Content-Type'] = 'text/javascript' response['Content-Type'] = mime_type
response.body = body response.body = body
end end


def build_body(path) 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('.') paths = path.gsub('/','').split('.')


until paths.empty? until paths.empty?
file = File.expand_path(paths.join('.')) file = File.expand_path(File.join(base, (paths).join('.')))
files << file if File.file?(file) files << file if File.file?(file)
paths.shift paths.shift
end end


body = "// dotjs is working! //\n" body = ''


files.each do |file| files.each do |file|
body << File.read(file) + "\n" if File.file?(file) body << File.read(file) + "\n" if File.file?(file)
end end


body [body, mime_type]
end end


def detect_origin(req) def detect_origin(req)
Expand All @@ -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] ssl_key = ssl_info.scan(/(-----BEGIN RSA PRIVATE KEY-----.+?-----END RSA PRIVATE KEY-----)/m)[0][0]


server_options = { server_options = {
:BindAddress => "127.0.0.1", :BindAddress => '127.0.0.1',
:Port => 3131, :Port => 3131,
:AccessLog => [], :AccessLog => [],
:SSLEnable => true, :SSLEnable => true,
:SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE, :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
:SSLPrivateKey => OpenSSL::PKey::RSA.new(ssl_key), :SSLPrivateKey => OpenSSL::PKey::RSA.new(ssl_key),
:SSLCertificate => OpenSSL::X509::Certificate.new(ssl_cert), :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) server = WEBrick::HTTPServer.new(server_options)
Expand Down
12 changes: 11 additions & 1 deletion ext/dotjs.js
@@ -1,5 +1,15 @@
$.ajax({ $.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('<style type="text/css">' + d + '</style>')
},
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', dataType: 'text',
success: function(d){ success: function(d){
$(function(){ eval(d) }) $(function(){ eval(d) })
Expand Down
20 changes: 12 additions & 8 deletions ext/manifest.json
Expand Up @@ -3,14 +3,18 @@
"manifest_version": 2, "manifest_version": 2,
"version": "2.0", "version": "2.0",
"description": "~/.js", "description": "~/.js",
"icons": { "48": "icon48.png", "icons": {
"128": "icon128.png" }, "48": "icon48.png",
"content_scripts": [{ "128": "icon128.png"
"all_frames": true, },
"run_at": "document_start", "content_scripts": [
"matches": ["http://*/*", "https://*/*"], {
"js": ["jquery.js", "dotjs.js"] "all_frames": true,
}], "run_at": "document_start",
"matches": ["http://*/*", "https://*/*"],
"js": ["jquery.js", "dotjs.js"]
}
],
"permissions": [ "permissions": [
"tabs" "tabs"
] ]
Expand Down