InjectJS

Inject javascript into almost every website you visit.

当前为 2022-12-03 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name InjectJS
  3. // @namespace http://github.com/YTXaos/InjectJS
  4. // @version 1.19
  5. // @description Inject javascript into almost every website you visit.
  6. // @author YTXaos
  7. // @match *://*/*
  8. // @icon https://raw.githubusercontent.com/YTXaos/InjectJS/main/assets/logo.png
  9. // @grant GM_addElement
  10. // @license MIT
  11. // @require https://code.jquery.com/jquery-3.6.0.min.js
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. "use strict";
  16. const url = location.href,
  17. origin = location.origin;
  18. function Option(item) {
  19. if(localStorage.getItem(`inject-js:${item}`)) {
  20. return localStorage.getItem(`inject-js:${item}`).toString();
  21. } else {
  22. return false;
  23. }
  24. }
  25. function onURL(page, mode) {
  26. switch(mode) {
  27. case "exact":
  28. return url === `${origin}${page}`;
  29. case "relative":
  30. return url.includes(page);
  31. default:
  32. console.error("InjectJS: Specify a matching mode.");
  33. }
  34. }
  35. if(onURL("/inject-js/", "exact")) {
  36. location = "https://github.com/YTXaos/InjectJS";
  37. }
  38. console.info("InjectJS Loaded. Press Ctrl + Q to topen");
  39. const popup = document.createElement("div"),
  40. style = document.createElement("style");
  41. fetch("https://raw.githubusercontent.com/YTXaos/InjectJS/main/assets/main.css").then(get => get.text()).then(set => style.innerHTML = set);
  42. popup.setAttribute("class", "js-injector-popup");
  43. popup.style.display = "none";
  44. popup.innerHTML = `<label class="js-inject-header">
  45. <div class="js-logo-needle">.....</div>
  46. Inject<span class="js-logo">JS</span>
  47. </label>
  48. <textarea placeholder="Your code here" class="js-code-inject" spellcheck="false" data-gramm="false" data-gramm_editor="false" data-enable-grammarly="false"></textarea>
  49. <div class="js-btns-section">
  50. <button class="execute-code" disabled>Execute</button>
  51. <button class="js-options-btn">Options</button>
  52. </div>`;
  53. document.head.prepend(style);
  54. document.body.prepend(popup);
  55.  
  56. function OptionsPage() {
  57. $("link[rel=stylesheet], style").remove();
  58. document.title = "InjectJS Options";
  59. fetch("https://raw.githubusercontent.com/YTXaos/InjectJS/main/pages/options.html").then(get => get.text()).then(set => document.body.innerHTML = set);
  60. fetch("https://raw.githubusercontent.com/YTXaos/InjectJS/main/options.js").then(get => get.text()).then(set => GM_addElement(document.head, "script", {
  61. textContent: set
  62. }));
  63. }
  64. const code = document.querySelector(".js-code-inject"),
  65. btn = document.querySelector(".execute-code"), option_btn = document.querySelector(".js-options-btn");
  66. code.addEventListener("input", CheckCode);
  67. btn.addEventListener("click", InjectCode);
  68. option_btn.addEventListener("click", () => { location = "/inject-js/options"; });
  69.  
  70. function CheckCode() {
  71. const code = document.querySelector(".js-code-inject");
  72. if(code.value.length < 5) {
  73. btn.setAttribute("disabled", "disabled");
  74. } else {
  75. btn.removeAttribute("disabled");
  76. }
  77. }
  78.  
  79. function InjectCode() {
  80. const code = document.querySelector(".js-code-inject").value;
  81. try {
  82. eval(code);
  83. } catch (e) {
  84. if(Option("alert_errors") == "true") {
  85. alert(e.message);
  86. } else {
  87. console.error(e.message);
  88. }
  89. }
  90. }
  91.  
  92. function ShowInjector() {
  93. dragElement(document.querySelector(".js-injector-popup"));
  94. function dragElement(elmnt) {
  95. var pos1 = 0,
  96. pos2 = 0,
  97. pos3 = 0,
  98. pos4 = 0;
  99. if(document.querySelector(".js-inject-header")) {
  100. document.querySelector(".js-inject-header").onmousedown = dragMouseDown;
  101. } else {
  102. elmnt.onmousedown = dragMouseDown;
  103. }
  104.  
  105. function dragMouseDown(e) {
  106. e = e || window.event;
  107. e.preventDefault();
  108. pos3 = e.clientX;
  109. pos4 = e.clientY;
  110. document.onmouseup = closeDragElement;
  111. document.onmousemove = elementDrag;
  112. }
  113.  
  114. function elementDrag(e) {
  115. e = e || window.event;
  116. e.preventDefault();
  117. pos1 = pos3 - e.clientX;
  118. pos2 = pos4 - e.clientY;
  119. pos3 = e.clientX;
  120. pos4 = e.clientY;
  121. elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
  122. elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  123. }
  124.  
  125. function closeDragElement() {
  126. document.onmouseup = null;
  127. document.onmousemove = null;
  128. }
  129. }
  130. popup.classList.toggle("show");
  131. }
  132. if(onURL("/inject-js/options", "exact")) {
  133. OptionsPage();
  134. }
  135. document.addEventListener("keyup", function(e) {
  136. e.preventDefault();
  137. if(e.ctrlKey && e.which === 81) {
  138. ShowInjector();
  139. }
  140. });
  141. })();