在当前页面加载链接网页

在当前页面加载链接网页,左滑后退

  1. // ==UserScript==
  2. // @name 在当前页面加载链接网页
  3. // @description 在当前页面加载链接网页,左滑后退
  4. // @include http://*
  5. // @include https://*
  6. // @author yechenyin
  7. // @version 1.0
  8. // @namespace https://gf.qytechs.cn/users/3586-yechenyin
  9. // @grant GM_openInTab
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. "use strict";
  14. var $ = function(selector, attributes) {
  15. if (selector.indexOf('<') === 0 || attributes) {
  16. var element;
  17. if (selector.indexOf('<') === 0)
  18. element = document.createElement(selector.substring(1, selector.length - 1));
  19. else
  20. element = document.createElement(selector);
  21.  
  22. if (attributes) {
  23. for (var key in attributes) {
  24. element[key] = attributes[key];
  25. }
  26. }
  27. return nodeToNodeList(element);
  28. } else {
  29. return document.querySelectorAll(selector);
  30. }
  31. };
  32. function nodeToNodeList(node) {
  33. return Object.create(document.createDocumentFragment().childNodes, {
  34. length: {
  35. value: 1
  36. },
  37. 0: {
  38. value: node,
  39. enumerable: true
  40. },
  41. });
  42. }
  43.  
  44.  
  45. NodeList.prototype.forEach = Array.prototype.forEach;
  46. NodeList.prototype.click = function(func) {
  47. this.forEach(function(node) {
  48. node.onclick = func;
  49. });
  50. };
  51. NodeList.prototype.on = function(eventName, func) {
  52. for (var i = 0; i < this.length; i++) {
  53. this[i].addEventListener(eventName, func);
  54. }
  55. };
  56. NodeList.prototype.append = function(element) {
  57. this.forEach(function(node) {
  58. for (var i = 0; i < element.length; i++) {
  59. node.appendChild(element[i]);
  60. }
  61. });
  62. };
  63. NodeList.prototype.appendTo = function(element) {
  64. if (typeof element == 'string')
  65. element = $(element);
  66. if (element instanceof NodeList) {
  67. this.forEach(function(node) {
  68. for (var i = 0; i < element.length; i++) {
  69. element[i].appendChild(node);
  70. }
  71. });
  72. }
  73. };
  74. NodeList.prototype.css = function(property, value) {
  75. this.forEach(function(node) {
  76. node.style[property] = value;
  77. });
  78. };
  79. NodeList.prototype.show = function() {
  80. this.css('display', 'block');
  81. };
  82. NodeList.prototype.last = function() {
  83. return nodeToNodeList(this[this.length - 1]);
  84. };
  85. NodeList.prototype.remove = function(func) {
  86. if (this && this.length > 0)
  87. Array.prototype.forEach.call(this, function(node) {
  88. node.parentNode.removeChild(node);
  89. });
  90. };
  91.  
  92. var load_process_bar;
  93. function open_tab(href) {
  94. if ((location.protocol == 'https:' && href.indexOf('http:') === 0) || $('.new_top_tab').length > 10) {
  95. window.open(href);
  96. } else {
  97. var tab = $('<iframe>', {
  98. src: href,
  99. className: 'new_top_tab',
  100. style: 'display:block; position:fixed; top:0; left:0; width:100%; height:100%; overflow:scroll; background:#fff; border: none; z-index: ' + String(2147483647 - 10 + $('.new_top_tab').length)
  101. });
  102. $('body').append(tab);
  103. tab[0].onload = function() {
  104. // this.style.display = 'block';
  105. // if (load_process_bar)
  106. // clearInterval(load_process_bar);
  107. // $('.tab_process_bar').remove();
  108. console.log($('body')[0]);
  109. };
  110.  
  111. if (load_process_bar)
  112. clearInterval(load_process_bar);
  113. $('.tab_process_bar').remove();
  114. $('<div>', {
  115. className: 'tab_process_bar',
  116. style: 'position:fixed; bottom:0; left:0; width:0; height:3px; border: none; background:#00ccff; z-index: 2147483647'
  117. }).appendTo('body');
  118. var loaded = 0;
  119. var load_process_bar = setInterval(function() {
  120. loaded++;
  121. if (loaded <= 100) {
  122. $('.tab_process_bar').css('width', loaded + '%');
  123. //var color = '000000' + Math.floor(parseInt('00ccff', 16) + (parseInt('9933ff', 16) - parseInt('00ccff', 16)) * loaded / 1000).toString(16);
  124. //$('.tab_process_bar').css('background', 'linear-gradient(to right, #00ccff, #' + color.substr(color.length - 6) + ')');
  125. } else if ($('.tab_process_bar').length === 0) {
  126. clearInterval(load_process_bar);
  127. }
  128. }, 200);
  129.  
  130. }
  131. }
  132.  
  133.  
  134. function getAncestorLink(element) {
  135. while (element.nodeName != "A") {
  136. element = element.parentNode;
  137. }
  138. if (element.nodeName === "A" && element.href && element.href.indexOf('javascript') !== 0)
  139. return element;
  140. }
  141.  
  142. document.addEventListener('click', function(e) {
  143. var link = getAncestorLink(e.target);
  144. if (link) {
  145. e.preventDefault();
  146. if (window.self === window.top) {
  147. open_tab(link.href);
  148. } else {
  149. window.parent.postMessage(JSON.stringify({
  150. open_href: link.href
  151. }), '*');
  152. }
  153. }
  154. });
  155.  
  156. if ('ontouchmove' in window && window.self !== window.top && $('title').length)
  157. console.log('touch in start');
  158. if (window.self !== window.top && $('title').length)
  159. window.parent.postMessage({
  160. loaded_href: location.href,
  161. loaded_title: document.title
  162. }, '*');
  163.  
  164. window.addEventListener('message', function(e) {
  165. if (e.data === 'go_back') {
  166. $('.tab_process_bar').remove();
  167. $('.new_top_tab').last().remove();
  168. } else if ($('.tab_process_bar').length > 0 && e.data.loaded_href) {
  169. $('.tab_process_bar').remove();
  170. console.log(e.data);
  171. var href = e.data.loaded_href;
  172. $('iframe[src="' + href + '"]').css('display', 'block');
  173. if (href.indexOf(location.origin) === 0) {
  174. history.pushState({
  175. href: location.href,
  176. title: $('title')[0].textContent
  177. }, null, location.href);
  178. history.replaceState(null, null, href.replace(location.origin, ''));
  179. document.title = e.data.loaded_title;
  180. }
  181. } else if (e.data.open_href) {
  182. open_tab(e.data.href);
  183. } else if (e.data.move_tab) {
  184. console.log(e.data);
  185. $('.new_top_tab').last().css('left', e.data.move_tab + 'px');
  186. }
  187. }
  188. );
  189.  
  190. window.addEventListener('popState', function(e) {
  191. if (history.state) {
  192. history.replaceState(null, null, e.state.href);
  193. document.title = e.state.title;
  194. console.log(document.title);
  195. }
  196. }
  197. );
  198.  
  199. var min_swipe_x = 50;
  200. var max_swipe_y = 30;
  201. var touch_start_x = 0;
  202. var touch_start_y = 0;
  203. var touch_move_x = 0;
  204. var touch_move_y = 0;
  205. document.addEventListener('touchstart', function(e) {
  206. touch_start_x = e.touches[0].clientX;
  207. touch_start_y = e.touches[0].clientY;
  208. });
  209. document.addEventListener('touchmove', function(e) {
  210. //e.preventDefault();
  211. touch_move_x = e.touches[0].clientX;
  212. touch_move_y = e.touches[0].clientY;
  213. //if (touch_move_x - touch_start_x > 20 && touch_move_x - touch_start_x < 120 && window.self !== window.top)
  214. // window.parent.postMessage({move_tab: touch_move_x - touch_start_x}, '*');
  215. }, {
  216. passive: true
  217. });
  218. document.addEventListener('touchend', function(e) {
  219. console.log(touch_move_x - touch_start_x + ' ' + Math.abs(touch_move_y - touch_start_y));
  220. if (touch_move_x - touch_start_x > min_swipe_x && Math.abs(touch_move_y - touch_start_y) < max_swipe_y)
  221. if (window.self !== window.top) {
  222. window.parent.postMessage('go_back', '*');
  223. } else {
  224. history.go(-1);
  225. }
  226.  
  227. });
  228.  
  229. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址