Escape special characters (e.g. '<', '>') in title text
[xkcd-keyboard-nav.git] / xkcd_keyboard_nav.user.js
index 4610ec9..0cfdcef 100644 (file)
@@ -1,6 +1,6 @@
 /* xkcd keyboard nav - Navigate xkcd with your keyboard
  * 
- * Copyright (c) 2015 Adrian Iain Lam <adrianiainlam@gmail.com>
+ * Copyright (c) 2015-2018 Adrian Iain Lam <me@adrianiainlam.tk>
  * 
  * This program is free software. It comes without any warranty, to
  * the extent permitted by applicable law. You can redistribute it
@@ -27,7 +27,7 @@
 // @name        xkcd keyboard nav
 // @namespace   https://github.com/adrianiainlam
 // @description Navigate xkcd with your keyboard
-// @version     1.0.2
+// @version     1.0.4
 // @downloadURL https://gist.github.com/adrianiainlam/542dd0794a874ca31321/raw/xkcd_keyboard_nav.user.js
 // @updateURL   https://gist.github.com/adrianiainlam/542dd0794a874ca31321/raw/xkcd_keyboard_nav.user.js
 // @include     /^https?://xkcd\.com(/[0-9]+)?/?$/
@@ -39,9 +39,9 @@ function showTitleText() {
   var child   = elem.children[0];
   var newnode = document.createElement("p");
   if(child instanceof HTMLImageElement) {
-      newnode.innerHTML = child.title;
+      newnode.appendChild(document.createTextNode(child.title));
   } else if(child instanceof HTMLAnchorElement && child.children[0] instanceof HTMLImageElement) {
-      newnode.innerHTML = child.children[0].title;
+      newnode.appendChild(document.createTextNode(child.children[0].title));
   } else {
       console.log("Title text not found by this script. Please file a bug report with URL");
   }
@@ -49,18 +49,16 @@ function showTitleText() {
 }
 
 document.body.addEventListener("keypress", function(e) {
-  if (e.defaultPrevented) {
-    return; // Should do nothing if the key event was already consumed.
+  if (!(e.target instanceof HTMLBodyElement)) {
+    return;
   }
   var navList = document.getElementsByClassName("comicNav")[0].children;
   if(!e.altKey && !e.ctrlKey && !e.metaKey && !e.shiftKey) {
     switch(e.key) {
       case "ArrowLeft": // prev
-      case "Left": // deprecated
         document.location.href = navList[1].firstChild.getAttribute("href");
         break;
       case "ArrowRight": // next
-      case "Right": // deprecated
         document.location.href = navList[3].firstChild.getAttribute("href");
         break;
       case "r": // random