Greasy Fork 还支持 简体中文。

InjectJS

Inject Javascript into almost any website.

目前為 2022-12-07 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name InjectJS
  3. // @namespace http://github.com/YTXaos/InjectJS
  4. // @version 1.23
  5. // @description Inject Javascript into almost any website.
  6. // @description:es Inyecte Javascript en casi cualquier sitio web
  7. // @description:fr Injectez Javascript dans presque tous les sites Web
  8. // @description:de Fügen Sie Javascript in fast jede Website ein
  9. // @description:ja ほぼすべてのウェブサイトにジャバスクリとを挿入する
  10. // @description:la Javascript in inject paene omnem website
  11. // @description:ru Внедрите Javascript практически в любой веб-сайт
  12. // @author YTXaos
  13. // @match *://*/*
  14. // @match file:///*
  15. // @icon https://raw.githubusercontent.com/YTXaos/InjectJS/main/assets/logo.png
  16. // @grant GM_addElement
  17. // @license MIT
  18. // @require https://code.jquery.com/jquery-3.6.0.min.js
  19. // ==/UserScript==
  20.  
  21. (function() {
  22. "use strict";
  23. const url = location.href,
  24. origin = location.origin;
  25. function Option(item) {
  26. if(localStorage.getItem(`inject-js:${item}`)) {
  27. return localStorage.getItem(`inject-js:${item}`).toString();
  28. } else {
  29. return false;
  30. }
  31. }
  32. function onURL(page, mode) {
  33. switch(mode) {
  34. case "exact":
  35. return url === `${origin}${page}`;
  36. case "relative":
  37. return url.includes(page);
  38. default:
  39. console.error("InjectJS: Specify a matching mode.");
  40. }
  41. }
  42. if(Option("disable") == "true") {
  43. document.addEventListener("keyup", function(e) {
  44. e.preventDefault();
  45. if(e.ctrlKey && e.key === "q") {
  46. location = "/inject-js/options";
  47. }
  48. });
  49. return;
  50. }
  51. if(onURL("/inject-js/", "exact")) {
  52. location = "https://github.com/YTXaos/InjectJS";
  53. }
  54. Option("startup_log") == "true" && (console.info("InjectJS Loaded. Press Ctrl + Q to topen"));
  55. const popup = document.createElement("div"),
  56. style = document.createElement("style");
  57. fetch("https://raw.githubusercontent.com/YTXaos/InjectJS/main/assets/main.css").then(get => get.text()).then(set => style.innerHTML = set);
  58. popup.setAttribute("class", "js-injector-popup");
  59. popup.style.display = "none";
  60. popup.innerHTML = `<label class="js-inject-header">
  61. <div class="js-logo-needle">.....</div>
  62. Inject<span class="js-logo">JS</span>
  63. </label>
  64. <textarea placeholder="Your code here" class="js-code-inject" spellcheck="false" data-gramm="false" data-gramm_editor="false" data-enable-grammarly="false" id="js-code-inject"></textarea>
  65. <div class="js-btns-section">
  66. <button class="execute-code" disabled>Execute</button>
  67. <button class="js-options-btn">Options</button>
  68. </div>`;
  69. document.head.prepend(style);
  70. document.body.prepend(popup);
  71.  
  72. function OptionsPage() {
  73. $("link[rel=stylesheet], style, script").remove();
  74. document.title = "InjectJS Options";
  75. fetch("https://raw.githubusercontent.com/YTXaos/InjectJS/main/pages/options.html").then(get => get.text()).then(set => document.body.innerHTML = set);
  76. fetch("https://raw.githubusercontent.com/YTXaos/InjectJS/main/options.js").then(get => get.text()).then(set => GM_addElement(document.head, "script", {
  77. textContent: set
  78. }));
  79. }
  80. const code = document.querySelector(".js-code-inject"),
  81. btn = document.querySelector(".execute-code"), option_btn = document.querySelector(".js-options-btn");
  82. code.addEventListener("input", CheckCode);
  83. code.addEventListener("keydown", Syntax);
  84. btn.addEventListener("click", InjectCode);
  85. option_btn.addEventListener("click", () => { location = "/inject-js/options"; });
  86.  
  87. function Syntax(e) {
  88. if(e.which === 219) {
  89. e.preventDefault();
  90. const start = code.selectionStart,
  91. end = code.selectionEnd,
  92. selection = code.value.substring(start, end),
  93. replace = `${code.value.substring(0, start)}{\n${selection}\n}${code.value.substring(end)}`;
  94. code.value = replace;
  95. code.focus();
  96. code.selectionEnd = end + 2;
  97. }
  98. if(e.which === 57) {
  99. e.preventDefault();
  100. const start = code.selectionStart,
  101. end = code.selectionEnd,
  102. selection = code.value.substring(start, end),
  103. replace = `${code.value.substring(0, start)}(${selection})${code.value.substring(end)}`;
  104. code.value = replace;
  105. code.focus();
  106. code.selectionEnd = end + 1;
  107. }
  108. if(e.which === 222) {
  109. e.preventDefault();
  110. const start = code.selectionStart,
  111. end = code.selectionEnd,
  112. selection = code.value.substring(start, end),
  113. replace = `${code.value.substring(0, start)}${e.key}${selection}${e.key}${code.value.substring(end)}`;
  114. code.value = replace;
  115. code.focus();
  116. code.selectionEnd = end + 1;
  117. }
  118. }
  119. function CheckCode() {
  120. const code = document.querySelector(".js-code-inject");
  121. if(code.value.length < 5) {
  122. btn.setAttribute("disabled", "disabled");
  123. } else {
  124. btn.removeAttribute("disabled");
  125. }
  126. }
  127.  
  128. function InjectCode() {
  129. const code = document.querySelector(".js-code-inject").value;
  130. try {
  131. eval(code);
  132. } catch (e) {
  133. if(Option("alert_errors") == "true") {
  134. alert(e.message);
  135. } else {
  136. console.error(`InjectJS: ${e.message}`);
  137. }
  138. }
  139. }
  140.  
  141. function ShowInjector() {
  142. dragElement(document.querySelector(".js-injector-popup"));
  143. function dragElement(elmnt) {
  144. var pos1 = 0,
  145. pos2 = 0,
  146. pos3 = 0,
  147. pos4 = 0;
  148. if(document.querySelector(".js-inject-header")) {
  149. document.querySelector(".js-inject-header").onmousedown = dragMouseDown;
  150. } else {
  151. elmnt.onmousedown = dragMouseDown;
  152. }
  153.  
  154. function dragMouseDown(e) {
  155. e = e || window.event;
  156. e.preventDefault();
  157. pos3 = e.clientX;
  158. pos4 = e.clientY;
  159. document.onmouseup = closeDragElement;
  160. document.onmousemove = elementDrag;
  161. }
  162.  
  163. function elementDrag(e) {
  164. e = e || window.event;
  165. e.preventDefault();
  166. pos1 = pos3 - e.clientX;
  167. pos2 = pos4 - e.clientY;
  168. pos3 = e.clientX;
  169. pos4 = e.clientY;
  170. elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
  171. elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  172. }
  173.  
  174. function closeDragElement() {
  175. document.onmouseup = null;
  176. document.onmousemove = null;
  177. }
  178. }
  179. popup.classList.toggle("show");
  180. }
  181. if(onURL("/inject-js/options", "exact")) {
  182. OptionsPage();
  183. }
  184. document.addEventListener("keyup", function(e) {
  185. e.preventDefault();
  186. if(e.ctrlKey && e.key === "q") {
  187. ShowInjector();
  188. }
  189. });
  190. })();