Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

add .css and image file support #44

Closed
wants to merge 2 commits into from

6 participants

@anithri

djsd changed to also serve .css files from ~/css. and .gif|.jpg|.png files from ~/.images

NOTE! no changes to the extensions were made!

I only added the ability to call http://localhost:3131/arbitrary.css or http://localhost:3131/some-pic.png
and have them resolve from ~/.css and ~/.images

This should allow including .css files from scripts, host icon or badge elements you might be throwing up locally.

I'm conflicted about changing the extension to automatically attempt to load http://localhost:3131/google.com.css seems like it might be a good idea, but i don't know what the performance hit would be.

anithri added some commits
@anithri anithri djsd changed to also serve .css files from ~/css. and .gif|.jpg|.png …
…files from ~/.images

 Rakefile changed to create ~/.css and ~/.images dirs
 README.markdown changed to reflect new bonus functionality

 NOTE!  no changes to the extensions were made.
50f1ece
@anithri anithri Merge branch 'master' of git://github.com/defunkt/dotjs 196cbd7
@sjl

This would be awesome.

@defunkt
Owner

I like it.

What do you think about using ~/.js/images and ~/.js/css instead? Having a single directory can be convenient.

@kennethreitz

~/.js/assets ?

@anithri

Sure I'm not concerned about where the files live so long as I can get them into my browser. I'm a lot more worried about how I would make those changes and update this pull request, I'm just a yellow belt in git fu.

@bartt

I'd like to load CSS files based on domain name just like JS files are loaded. I can provide a pull request that uses ~/.js/css and ~/.js/images AND modifies the Chrome extension to also load CSS files. Should I just submit a different pull request?

@ralesi

I second bartt's preference, I am a big fan of this in the FF port of dotjs.

@defunkt
Owner

closing unless someone wants to update the paths

@defunkt defunkt closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 9, 2011
  1. @anithri

    djsd changed to also serve .css files from ~/css. and .gif|.jpg|.png …

    anithri authored
    …files from ~/.images
    
     Rakefile changed to create ~/.css and ~/.images dirs
     README.markdown changed to reflect new bonus functionality
    
     NOTE!  no changes to the extensions were made.
Commits on Jul 13, 2011
  1. @anithri
This page is out of date. Refresh to see the latest.
Showing with 43 additions and 8 deletions.
  1. +6 −0 README.markdown
  2. +11 −1 Rakefile
  3. +26 −7 bin/djsd
View
6 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,
View
12 Rakefile
@@ -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"
@@ -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
View
33 bin/djsd
@@ -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
Something went wrong with that request. Please try again.