Skip to content

Instantly share code, notes, and snippets.

@chrismytton
Created February 17, 2012 23:11
Show Gist options
  • Save chrismytton/1856012 to your computer and use it in GitHub Desktop.
Save chrismytton/1856012 to your computer and use it in GitHub Desktop.
bookmarklet creater
/node_modules
fs = require 'fs'
coffee = require 'coffee-script'
uglify = require 'uglify-js'
file = process.argv[2]
unless file?
console.warn "Usage: bookmarklet <script.{js,coffee}>"
process.exit 1
fs.readFile file, 'utf8', (err, data) ->
throw err if err
if file.match /\.coffee$/
console.log "javascript:#{encodeURI uglify coffee.compile data}"
else
console.log "javascript:#{encodeURI uglify data}"
#!/usr/bin/env node
require('coffee-script');
require('./bookmarklet');
{
"author": "",
"name": "bookmarklet",
"description": "Easily create bookmarklets",
"version": "0.0.0",
"bin": "./cli.js",
"repository": {
"type": "git",
"url": "git://gist.github.com/1856012.git"
},
"engines": {
"node": ">= 0.4.0"
},
"dependencies": {
"coffee-script": "1.2.0",
"uglify-js": "1.2.5"
},
"devDependencies": {},
"optionalDependencies": {}
}
@chrismytton
Copy link
Author

To install this with npm(1) run the following:

$ npm install -g git://gist.github.com/1856012.git

Then you can use the bookmarklet script:

$ cat example.coffee
iframeSource = 'http://example.com/bookmarklet'

iframe = document.createElement('iframe')
iframe.setAttribute 'src', iframeSource
iframe.setAttribute 'id', 'rusic-modal'
iframe.setAttribute 'style', 'position: fixed; z-index: 999999; width: 500px; height: 300px; right: 10px; top: 10px; border: 5px solid #e1e1e1; overflow: hidden; background-color: #fff;'
body = document.getElementsByTagName('body')[0]
body.appendChild iframe

$ bookmarklet example.coffee
javascript:((function()%7Bvar%20a,b,c;c=%22http://example.com/bookmarklet%22,b=document.createElement(%22iframe%22),b.setAttribute(%22src%22,c),b.setAttribute(%22id%22,%22rusic-modal%22),b.setAttribute(%22style%22,%22position:%20fixed;%20z-index:%20999999;%20width:%20500px;%20height:%20300px;%20right:%2010px;%20top:%2010px;%20border:%205px%20solid%20#e1e1e1;%20overflow:%20hidden;%20background-color:%20#fff;%22),a=document.getElementsByTagName(%22body%22)%5B0%5D,a.appendChild(b)%7D)).call(this)

$ bookmarklet example.coffee | pbcopy # This puts the bookmarklet on your clipboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment