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

add .css and image file support #44

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions README.markdown
Expand Up @@ -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
Expand All @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion Rakefile
Expand Up @@ -60,12 +60,20 @@ namespace :install do
cp "bin/djsd", DAEMON_INSTALL_DIR, :verbose => true, :preserve => true
end

desc "Create ~/.js"
desc "Create ~/.js, ~/.css, and ~/.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'], ".css")
mkdir css_dir
chmod 0755, css_dir
end
if !File.directory? images_dir = File.join(ENV['HOME'], ".images")
mkdir images_dir
chmod 0755, images_dir
end
end

desc "Install Google Chrome extension"
Expand All @@ -90,6 +98,8 @@ namespace :uninstall do
puts "3. The 'dotjs' Google Chrome Extension",""
puts "I will not remove:", ""
puts "1. ~/.js", ""
puts "2. ~/.css", ""
puts "3. ~/.images", ""
print "Ok? (y/n) "

begin
Expand Down
33 changes: 26 additions & 7 deletions bin/djsd
Expand Up @@ -14,17 +14,36 @@ end
require 'webrick'

dotjs = Class.new(WEBrick::HTTPServlet::AbstractServlet) do
def do_GET(request, response)
file = File.expand_path("#{request.path.gsub('/','')}")
default = File.expand_path("default.js")
IMAGE_TYPES = %w(.png .jpg .gif)

body = "// dotjs is working! //\n"
body << File.read(default) + "\n" if File.file?(default)
body << File.read(file) if File.file?(file)
def do_GET(request, response)
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("~/.css/default.css")
files << File.expand_path("~/.css/#{request.path.gsub('/','')}")
mime_type = 'text/css'
when IMAGE_TYPES.include?(ext)
files << File.expand_path("~/.images/#{request.path.gsub('/','')}")
mime_type = "image/#{ext[1 .. -1]}"
else
puts "oops"
end

body = ''
files.each do |f|
body << File.read(f) if File.exists?(f)
body << "\n"
end

response.status = body.empty? ? 204 : 200
response['Access-Control-Allow-Origin'] = '*'
response['Content-Type'] = 'text/javascript'
response['Content-Type'] = mime_type
response.body = body
end
end
Expand Down