Console Importer

通过控制台导入 JS / CSS 库

当前为 2023-09-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Console Importer
  3. // @name:en Console Importer
  4. // @description 通过控制台导入 JS / CSS 库
  5. // @description:en Import JavaScript or CSS Library in browser developer tool.
  6. // @author Yiero
  7. // @version 1.0.0
  8. // @match https://*/*
  9. // @run-at document-body
  10. // @license GPL
  11. // @icon https://bbs.tampermonkey.net.cn/favicon.ico
  12. // @grant GM_addElement
  13. // @grant GM_xmlhttpRequest
  14. // @namespace https://github.com/AliubYiero/TamperMonkeyScripts/
  15. // @connect cdnjs.cloudflare.com
  16. // @connect cdn.bootcdn.net
  17. // @connect cdn.jsdelivr.net
  18. // ==/UserScript==
  19. function isCSS( url ) {
  20. return url.endsWith( "css" );
  21. }
  22.  
  23. const urlList = new Map;
  24.  
  25. function getRequire( url ) {
  26. if ( urlList.has( url ) ) {
  27. console.error( "[%s] \u83b7\u53d6\u6570\u636e\u5931\u8d25...\n\u5f53\u524d\u9875\u9762\u4e2d\u5df2\u5b58\u5728\u5e93 [%s]", "Console Importer", url );
  28. return;
  29. }
  30. GM_xmlhttpRequest( {
  31. method: "GET",
  32. url: url,
  33. onload( e ) {
  34. const scriptText = e.responseText;
  35. const isScript = isCSS( url );
  36. const element = GM_addElement( document.head, isScript ? "script" : "style", {
  37. textContent: scriptText
  38. } );
  39. urlList.set( url, element.id );
  40. },
  41. onerror( e ) {
  42. console.error( "[%s] \u83b7\u53d6\u6570\u636e\u5931\u8d25...\n", "Console Importer", e );
  43. }
  44. } );
  45. }
  46.  
  47. ( () => {
  48. unsafeWindow.$i = getRequire;
  49. } )();