Permalink
Browse files

Better, more intuitive test page

  • Loading branch information...
1 parent 3241403 commit e3ebf72faaff456d31eba374fd8d7a281ecf491c @jonnay jonnay committed Jul 6, 2012
Showing with 96 additions and 4 deletions.
  1. +25 −1 client/test.html
  2. +8 −1 slime-js.el
  3. +20 −0 swank-handler.js
  4. +43 −2 swank.js
View
@@ -1,8 +1,32 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
+ <title> Swank JS test page </title>
<meta http-equiv="X-UA-Compatible" content="IE=9">
<script type="text/javascript" src="/swank-js/swank-js-inject.js"></script>
</head>
- <body></body>
+ <body>
+ <h1> You have connected to swank.js! </h1>
+ <p> you can use ,select-remote to select this browser now! </h2>
+
+ <h2> Bookmarklets </h2>
+ <p> Here are some bookmarklets you can use to inject the swank
+ javascript on this page:</p>
+ <ul>
+ <!--[[bookmarklets]]-->
+ <li><a href="javascript:(function()%7BSwankJS.disconnect()%3B%7D)()%3B">swank
+ disconnect</a> </li>
+ </ul>
+
+ <h3> Adding a bookmarklet on iOS </h3>
+ <p> It's kind of a pain to do this. Thanks to <a href="http://static.chrisbray.com/bookmarklets/">Chris Bray</a> for
+ figuring this out! </p>
+ <ol>
+ <li> Add this page as a bookmark </li>
+ <li> Select and copy the bookmarklet text </li>
+ <li> Go to your bookmarks and tap edit the one you just made </li>
+ <li> Edit the name (if you like) and paste the URL.</li>
+ <li> Profit! </li>
+ </ol>
+ </body>
</html>
View
@@ -44,7 +44,9 @@
(defcustom slime-js-swank-args '("run" "swank")
"Command arguments for running the swank-js server from node.js.
-Note that file paths need to be complete file paths, i.e. ~ to /home/you or /Uesrs/you."
+Note that file paths need to be complete file paths, i.e. ~ to /home/you or /Uesrs/you.
+If you are using npm, then you probably want this to have 2 values: \"run\" \"swank\".
+If you want swank-js to run on a differnet port, add it as the third element to this list."
:type '(repeat (string :tag "Arg"))
:group 'slime-js)
@@ -97,6 +99,11 @@ Note that file paths need to be complete file paths, i.e. ~ to /home/you or /Ues
(defvar slime-js-remote-history nil
"History list for JS remote names.")
+(defun slime-js-browse-test ()
+ "Interactive funciton to open the test page locally"
+ (interactive)
+ (browse-url "http://localhost:8009/client/test.html"))
+
(defun slime-js-read-remote-index (&optional prompt)
(let* ((completion-ignore-case nil)
(remotes (slime-eval '(js:list-remotes)))
View
@@ -49,6 +49,21 @@ function Handler (executive) {
util.inherits(Handler, EventEmitter);
+/**
+ * A simple object containing a hash table of handler-names and handlers
+ * A handler name is a simply transformed command from lisp.
+ * The "swank:" is removed, and - is turned into _
+ * so "swank:quit-lisp" becomes "quit_lisp".
+ *
+ */
+Handler.prototype.messageHandlers = {};
+
+Handler.prototype.messageHandlers.quit_lisp = function(f) {
+ // TBD Maybe the remotes should be contacted so they can shut themselves down gracefully as well?
+ console.log("Quitting Swank!");
+ process.exit(0);
+}
+
Handler.prototype.receive = function receive (message) {
// FIXME: error handling
console.log("Handler.prototype.receive(): %s", repr(message).replace(/\n/, "\\n"));
@@ -184,6 +199,11 @@ Handler.prototype.receive = function receive (message) {
});
return;
default:
+ var method = d.form.name.split(":")[1].replace(/-/g,'_'); // FIXME Brittle code, Expects ":" to be in the form name
+ console.log("Unfound Command, Trying to run: "+method);
+ if (this.messageHandlers.hasOwnProperty(method)) {
+ this.messageHandlers[method](d.form);
+ }
// FIXME: handle unknown commands
}
cont();
View
45 swank.js 100644 → 100755
@@ -346,13 +346,48 @@ HttpListener.prototype.doProxyRequest = function doProxyRequest (targetUrl, requ
});
};
+HttpListener.prototype.getBookMarklets = function getBookMarklets() {
+ //TBD: Allow IPv6 Bookmarklets?
+ var ifaces = require('os').networkInterfaces();
+ var ips = [];
+
+ //TBD: This sucks and can probably be done much better in a functional style
+ for (dev in ifaces) {
+ console.log ("found device "+dev);
+ ifaces[dev].forEach(function(details) {
+ console.log(details);
+ if (details.family=='IPv4') {
+ ips.push(details.address);
+ }
+ });
+ }
+
+ //console.log(ips);
+
+ //TODO: the port is a magic number. Needs to stop being magic.
+ var out = ips.map(function(ip){
+ var bookmarklet = escape("(function(d){window.swank_server='http://"+ip+":8009/';if(!d.getElementById('swank-js-inj')){var h=d.getElementsByTagName('head')[0],s=d.createElement('script');s.id='swank-js-inj';s.type='text/javascript';s.src=swank_server+'swank-js/swank-js-inject.js';h.appendChild(s);}})(document);");
+ return '<li><a href="javascript:'+bookmarklet +'"> Connet to slime on '+ip+'</a><br/>javascript:'+bookmarklet+'</li>';
+ });
+ //console.log(out.join('\n'));
+ return out.join('\n');
+}
+
HttpListener.prototype.sendCachedFile = function sendCachedFile (req, res, path) {
if (req.headers['if-none-match'] == this.clientVersion) {
res.writeHead(304);
res.end();
} else {
+ // sorry for this, but there is only one replacement, and only one file to replace, so
+ // for now... some bad code. But if there needs to be a new replacement, or replacements
+ // on more then one file, this'll need updating.
+ var out = ((path == 'client/test.html') ? (this.cachedFiles[path].content+'').replace('<!--[[bookmarklets]]-->',this.getBookMarklets())
+ : this.cachedFiles[path].content);
+ //console.log(out);
+ //TBD: Remove the setting of the length header earlier on in the process.
+ this.cachedFiles[path].headers['Content-Length'] = Buffer(out).length;
res.writeHead(200, this.cachedFiles[path].headers);
- res.end(this.cachedFiles[path].content, this.cachedFiles[path].encoding);
+ res.end(out, this.cachedFiles[path].encoding);
}
};
@@ -366,10 +401,13 @@ HttpListener.prototype.serveClient = function serveClient(req, res) {
var path = url.parse(req.url).pathname, parts, cn;
// console.log("%s %s", req.method, req.url);
if (path && path.indexOf("/swank-js/") != 0) {
- // console.log("--> proxy");
+ //console.log("--> proxy");
this.proxyRequest(req, res);
return;
}
+
+
+ //console.log("--> internal");
var file = path.substr(1).split('/').slice(1);
var localPath = this.clientFiles[file];
if (req.method == 'GET' && localPath !== undefined) {
@@ -387,6 +425,9 @@ HttpListener.prototype.serveClient = function serveClient(req, res) {
} else {
var ext = localPath.split('.').pop();
self.cachedFiles[localPath] = {
+ // right now there is no difference between cached files
+ // and files inside of the client dir. That should probably change
+ // soon.
headers: {
'Content-Length': data.length,
'Content-Type': self.types[ext],

0 comments on commit e3ebf72

Please sign in to comment.