I just released a small piece of code called dotjs-pow on GitHub, before I tell you what it is, let me tell you a short story first.
You see, recently, I read a lot of books. I bought most of them on amazon.cn since you know, dead-tree versions of Chinese books are dirt cheap.
And also, there is this Chinese site douban.com, which has generally pretty reliable Chinese book ratings and reviews, and you know what, they have some nice APIs too!
Since I find myself constantly switching Safari tabs between pages from amazon.cn and douban.com, I started to think that how can I make this easier.
Initially, I thought about building a Safari extension, but it is not an elegant solution.
Then I found this awesome piece of code called dotjs by GitHub CEO(!) Chris Wanstrath (a.k.a. defunkt), basically it lets you run a custom js file located in your
~/.js each time you visit a site.
“Yes!”, I think, so in a hurry I installed it (albeit I have to install dotjs.safariextension as well since defunkt was a Chrome guy) and put following code in my
~/.js/amazon.cn.js to put it to a test:
1 2 3 4 5 6 7 8 9 10 11 12 13
And points my Safari to amazon.cn. As you might have already guessed, I saw this in Safari console:
Since I don’t control either amazon.cn or douban.com, one solution is that I build an API proxy for api.douban.com.
It’s quite easy to do so using Sinatra and HTTParty:
1 2 3 4 5 6 7 8 9 10
But of course I don’t want to put the proxy on the Internet! So am I going to run yet another service on my localhost? Hmm, I don’t like ugly hacks.
Then I realized
djsd used by dotjs is just a static file server, and since I already have a Rack-compliant Pow running on my machine (as many Rubyists do), why not just build a Pow powered dotjs server, which can also be used to bypass cross-site security protections as well?
Thus dotjs-pow was born.
And with following codes in my
~/.js/amazon.cn.js, job’s done.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
Check it out in action (please forgive me, I suck at styling web pages…):
Oh, of course I have to use a modified version of dotjs.safariextension since I changed the server address from http://localhost:3131 to http://dotjs-pow.dev.
The full source code of app.rb (that is, the dotjs-pow server) is as following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
If you are a hopeless nerd like me, you might want to check it out yourself, visit dotjs-pow on GitHub now.