├── .gitignore ├── README.md ├── bin ├── bookmarkletify └── cli.js ├── bookmarkletify.js ├── package.json └── test ├── cli-input.js ├── test-cli.js └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bookmarkletify 2 | 3 | convert javascript source into the format needed for a bookmarklet 4 | 5 | ## Usage 6 | 7 | As a **module** 8 | 9 | ```sh 10 | npm install bookmarkletify 11 | 12 | ``` 13 | 14 | ```js 15 | var bookmarkletify = require('bookmarkletify'); 16 | var source = 'alert( window.location )'; 17 | var bookmarkletString = bookmarkletify(source); 18 | 19 | console.log(bookmarkletString); //javascript:(function(){;alert(window.location);})() 20 | ``` 21 | 22 | or as a **command line tool** 23 | 24 | ```sh 25 | npm install -g bookmarkletify 26 | bookmarkletify input.js -o output.bookmarklet 27 | 28 | ``` 29 | 30 | ## license 31 | 32 | MIT 33 | -------------------------------------------------------------------------------- /bin/bookmarkletify: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var nomnom = require('nomnom') 4 | var cli = require('./cli') 5 | 6 | var opts = nomnom 7 | .script("bookmarkletify") 8 | .options({ 9 | infile:{ 10 | position: 0, 11 | help: "js file to bookmarkletify" 12 | }, 13 | outfile: { 14 | abbr: 'o', 15 | help: "Write the bookmarklet to this file.\nIf unspecified, prints to stdout." 16 | } 17 | }) 18 | .nocolors() 19 | .nom() 20 | 21 | cli(opts) -------------------------------------------------------------------------------- /bin/cli.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs') 2 | var concat = require('concat-stream') 3 | var streamify = require('string-to-stream') 4 | var bookmarkletify = require('../bookmarkletify') 5 | 6 | module.exports = function (opts, cb) { 7 | cb = cb || function () {} 8 | var input = opts.infile ? fs.createReadStream(opts.infile) : process.stdin 9 | var output = opts.outfile ? fs.createWriteStream(opts.outfile) : process.stdout 10 | input.on('error', boom) 11 | output.on('error', boom) 12 | output.on('close', cb) 13 | input.pipe(concat(function (buffer) { 14 | // TODO: bookmarkletify could be a stream transform... 15 | streamify(bookmarkletify(buffer.toString()) + '\n').pipe(output) 16 | })) 17 | } 18 | 19 | function boom (err) { 20 | console.error(err) 21 | process.exit(1) 22 | } 23 | 24 | -------------------------------------------------------------------------------- /bookmarkletify.js: -------------------------------------------------------------------------------- 1 | var jsp = require("uglify-js").parser; 2 | var pro = require("uglify-js").uglify; 3 | 4 | module.exports = function(source){ 5 | var min = encodeURI(minify(source)); 6 | var result = 'javascript:(function(){;' + min + ';})()'; 7 | return result; 8 | } 9 | 10 | function minify(source){ 11 | var ast = jsp.parse(source); 12 | return pro.gen_code(ast); 13 | } 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bookmarkletify", 3 | "version": "1.0.0", 4 | "description": "From a single javascript file, create a bookmarklet", 5 | "main": "bookmarkletify.js", 6 | "bin": { 7 | "bookmarkletify": "bin/bookmarkletify" 8 | }, 9 | "scripts": { 10 | "test": "vows test/test*" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/johnkpaul/bookmarkletify.git" 15 | }, 16 | "author": "John K. Paul (http://twitter.com/johnkpaul)", 17 | "contributors": [ 18 | "Oli Evans (http://twitter.com/olizilla)" 19 | ], 20 | "license": "MIT", 21 | "keywords": [ 22 | "bookmarklet" 23 | ], 24 | "dependencies": { 25 | "concat-stream": "^1.4.7", 26 | "nomnom": "^1.8.1", 27 | "string-to-stream": "^1.0.0", 28 | "uglify-js": "~1.3.4", 29 | "vows": "~0.6.4" 30 | }, 31 | "devDependencies": { 32 | "vows": "^0.8.1" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /test/cli-input.js: -------------------------------------------------------------------------------- 1 | alert("http://google.com/test test"); -------------------------------------------------------------------------------- /test/test-cli.js: -------------------------------------------------------------------------------- 1 | var cli = require('../bin/cli'); 2 | var fs = require('fs'); 3 | var path = require('path') 4 | var vows = require('vows'); 5 | var assert = require('assert'); 6 | var infile = path.join(__dirname,'cli-input.js') 7 | var outfile = path.join(__dirname,'cli-output.txt') 8 | 9 | vows.describe('bookmarkletify cli').addBatch({ 10 | 'when passed a javascript file': { 11 | topic: function () { 12 | cleanup() 13 | var opts = { 14 | infile: infile, 15 | outfile: outfile 16 | } 17 | cli(opts, this.callback); 18 | }, 19 | 'writes a bookmarklet to the outfile': function (topic) { 20 | var txt = fs.readFileSync(outfile, "utf8") 21 | assert.equal(txt, 'javascript:(function(){;alert(%22http://google.com/test%20test%22);})()\n'); 22 | cleanup() 23 | } 24 | } 25 | }).export(module); 26 | 27 | 28 | function cleanup () { 29 | if (fs.existsSync(outfile)) fs.unlinkSync(outfile) // remove the test output if there. 30 | } 31 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var bookmarkletify = require('../bookmarkletify'); 2 | 3 | var vows = require('vows'); 4 | var assert = require('assert'); 5 | 6 | vows.describe('bookmarkletify').addBatch({ 7 | 'when passed a javascript string': { 8 | topic: function () { 9 | return bookmarkletify('alert( window.location )'); 10 | }, 11 | 'returns a bookmarklet': function (topic) { 12 | assert.equal(topic, 'javascript:(function(){;alert(window.location);})()'); 13 | } 14 | }, 15 | 'when passed a javascript string with non-url-safe characters': { 16 | topic: function () { 17 | return bookmarkletify('alert("http://google.com/test test");'); 18 | }, 19 | 'returns a url-safe bookmarklet': function (topic) { 20 | assert.equal(topic, 'javascript:(function(){;alert(%22http://google.com/test%20test%22);})()'); 21 | } 22 | } 23 | }).export(module); 24 | --------------------------------------------------------------------------------