split view

originated from v2ex user @v2yllhwa

当前为 2023-03-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name split view
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description originated from v2ex user @v2yllhwa
  6. // @author You
  7. // @match https://*/*
  8. // @match http://*/*
  9. // @match ftp://*/*
  10. // @grant none
  11. // @run-at document-end
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. "use strict";
  17. let url = window.location.href;
  18. if (window.self === window.top) {
  19. document.write(
  20. `
  21. <html>
  22. <head>
  23. </head>
  24.  
  25. <body style="margin:0;padding:0;background:gray;" scroll="no">
  26. <iframe src='${url}' id='leftpage' style='width:49%;height:100%;float:left' frameborder='0' allowfullscreen='true' sandbox="allow-downloads allow-downloads-without-user-activation allow-presentation allow-modals allow-same-origin allow-scripts allow-popups allow-forms allow-pointer-lock allow-top-navigation-by-user-activation"></iframe>
  27. <iframe src='' id='rightpage' style='width:49%;height:100%;float:right' frameborder='0' allowfullscreen='true' sandbox="allow-downloads allow-downloads-without-user-activation allow-presentation allow-modals allow-same-origin allow-scripts allow-popups allow-forms allow-pointer-lock allow-top-navigation-by-user-activation"></iframe>
  28. </body>
  29.  
  30. <script>
  31. var lefturl = '${url}';
  32. var righturl;
  33. var iframe_open_url = function (left, right) {
  34. if(righturl != right){
  35. righturl = right;
  36. document.getElementById('rightpage').src = righturl;
  37. }
  38. if(lefturl != left){
  39. lefturl = left;
  40. document.getElementById('leftpage').src = lefturl;
  41. }
  42. history.replaceState({},"",righturl);
  43. history.replaceState({},"",lefturl);
  44. }
  45. </script>
  46. </html>
  47. `
  48. );
  49. } else {
  50. document.body.style.minWidth = "unset";
  51. let nodes = document.querySelectorAll("a");
  52. for (let i = 0; i < nodes.length; i++) {
  53. // console.log(nodes[i].href);
  54. nodes[i].addEventListener("click", function (e) {
  55. e.preventDefault();
  56. e.stopPropagation();
  57. window.parent.iframe_open_url(url, nodes[i].href);
  58. });
  59. }
  60. }
  61. })();