]> git.scottworley.com Git - reliable-chat/commitdiff
URL detection
authorScott Worley <sworley@chkno.net>
Sat, 4 Aug 2012 20:00:11 +0000 (13:00 -0700)
committerScott Worley <sworley@chkno.net>
Sat, 4 Aug 2012 20:00:11 +0000 (13:00 -0700)
webclient/rc.html

index d91d3a611c9e069c26ad1c2746dd0d46da63a3d3..36c0c10663c89a7d955af2f6c26fbfb141048317 100644 (file)
@@ -31,6 +31,9 @@
                color: silver;
                font-family: monospace;
        }
                color: silver;
                font-family: monospace;
        }
+       a {
+               color: white;
+       }
        #container {
                height: 100%;
        }
        #container {
                height: 100%;
        }
                        type += " self";
                }
                text_span.setAttribute("class", type);
                        type += " self";
                }
                text_span.setAttribute("class", type);
-               text_span.appendChild(document.createTextNode(message.Text));
+
+               // URL detection
+               var text = message.Text;
+               var URL_re = /\bhttps?:\/\/\S+/;
+               while (URL_re.test(text)) {
+                       var match = URL_re.exec(text);
+                       var leading_text = text.substr(0, match.index);
+                       if (leading_text) {
+                               text_span.appendChild(document.createTextNode(leading_text));
+                       }
+                       var anchor = document.createElement("a");
+                       anchor.setAttribute("href", encodeURI(match[0]));
+                       anchor.appendChild(document.createTextNode(match[0]));
+                       text_span.appendChild(anchor);
+                       text = text.substr(match.index + match[0].length);
+               }
+               if (text) {
+                       text_span.appendChild(document.createTextNode(text));
+               }
+
                message.UI.appendChild(text_span);
        }
 
                message.UI.appendChild(text_span);
        }