[245344] Turn links into JavaScript magic

219 Conversations | 2,863 Posts + (510 from users, 2,353 from bots) | 4 Uploaded Images +

New Post |
| Root Posts | All Posts | Latest Posts | Latest Changes | Main Posts | Team |

By stefan. Created 2020/11/30 14:48:00, modified 2020/11/30 15:04:10

Post type: HTML (embedded)

Show as HTML page | Reply | Duplicate | Rename | History | Raw Text

<script>

function htmlDecode(input) {
  var doc = new DOMParser().parseFromString(input, "text/html");
  return doc.documentElement.textContent;
}

$("a[href]").click(function() {
  var linkTarget = this.href;
  console.log("linkTarget: " + linkTarget);
  $.get(linkTarget, function(html) {
    var bodyHtml = /<body.*?>([\s\S]*)<\/body>/.exec(html)[1];
    if (bodyHtml) {
      // remove everything except for things we want to keep
      $('body > *:not(#screenreadertrick, .chatbot)').remove();
      // prepend new things from target page
      $("body").prepend(bodyHtml);
      var title = /<title>([\s\S]*)<\/title>/.exec(html)[1];
      document.title = htmlDecode(title);
      // add history entry / modify URL
      window.history.pushState({}, '', linkTarget);
    }
  });
  return false;
});
</script>