split view

originated from v2ex user @v2yllhwa

目前为 2023-03-04 提交的版本。查看 最新版本

  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. // ==/UserScript==
  13.  
  14. (function () {
  15. "use strict";
  16. let url = window.location.href;
  17. if (window.self === window.top) {
  18. document.write(
  19. `
  20. <html>
  21. <head>
  22. </head>
  23.  
  24. <body style="margin:0;padding:0;background:gray;" scroll="no">
  25. <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>
  26. <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>
  27. </body>
  28.  
  29. <script>
  30. var lefturl = '${url}';
  31. var righturl;
  32. var iframe_open_url = function (left, right) {
  33. if(righturl != right){
  34. righturl = right;
  35. document.getElementById('rightpage').src = righturl;
  36. }
  37. if(lefturl != left){
  38. lefturl = left;
  39. document.getElementById('leftpage').src = lefturl;
  40. }
  41. history.replaceState({},"",righturl);
  42. history.replaceState({},"",lefturl);
  43. }
  44. </script>
  45. </html>
  46. `
  47. );
  48. } else {
  49. document.body.style.minWidth = "unset";
  50. let nodes = document.querySelectorAll("a");
  51. for (let i = 0; i < nodes.length; i++) {
  52. // console.log(nodes[i].href);
  53. nodes[i].addEventListener("click", function (e) {
  54. e.preventDefault();
  55. e.stopPropagation();
  56. window.parent.iframe_open_url(url, nodes[i].href);
  57. });
  58. }
  59. }
  60. })();