importShell

一个控制台导包工具

  1. // ==UserScript==
  2. // @name importShell
  3. // @namespace http://github.com/2943102883
  4. // @version 1.0.1
  5. // @description 一个控制台导包工具
  6. // @author SunShineGo
  7. // @license MIT
  8. // @include *://*
  9. // @require https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. ;(function () {
  14. 'use strict'
  15. defaultInsert.pkg = name => {
  16. insertJS(name, 'pkg')
  17. }
  18. defaultInsert.cdn = name => {
  19. insertJS(name, 'cdn')
  20. }
  21. defaultInsert.esm = name => {
  22. insertJS(name, 'esm')
  23. }
  24. window.$i = defaultInsert
  25. })()
  26.  
  27. function defaultInsert(name) {
  28. insertJS(name, 'pkg')
  29. }
  30. async function insertJS(name, type = 'pkg') {
  31. if (!help(name)) return
  32. const linkDir = {
  33. pkg: await pkg(name),
  34. cdn: await cdn(name),
  35. esm: await esm(name)
  36. }
  37. if (typeof name != 'string') throw new Error('请输入正确的包名')
  38. // 引入js
  39. var script = document.createElement('script')
  40. script.type = 'text/javascript'
  41. const link = linkDir[type]
  42. script.src = link
  43. console.log('%c[script]:加载中...', 'color:red')
  44. script.onload = function () {
  45. console.log(
  46. `%c [script]: %c${name} %c加载成功`,
  47. 'color:blue',
  48. 'color:blue;font-weight:600;font-size: 15px',
  49. 'color: blue'
  50. )
  51. }
  52. script.onerror = function () {
  53. console.error(
  54. `%c [script]: %c${name} %c无效的包名或加载失败`,
  55. 'color:blue',
  56. 'color:red;font-weight:600;font-size: 15px',
  57. 'color: blue'
  58. )
  59. }
  60. document.head.appendChild(script)
  61. }
  62.  
  63. function pkg(name) {
  64. return new Promise((resolve, reject) => {
  65. resolve(`https://unpkg.com/${name}`)
  66. })
  67. }
  68.  
  69. function cdn(name) {
  70. return new Promise((resolve, reject) => {
  71. fetch(`https://api.cdnjs.com/libraries?search=${name}`, { referrerPolicy: 'no-referrer' })
  72. .then(t => t.json())
  73. .then(res => {
  74. if (res.results && res.results.length > 0) resolve(res.results[0].latest)
  75. else resolve(false)
  76. })
  77. })
  78. }
  79.  
  80. function esm(name) {
  81. return new Promise((resolve, reject) => {
  82. resolve(`https://cdn.jsdelivr.net/npm/${name}`)
  83. })
  84. }
  85.  
  86. function help(code) {
  87. switch (code) {
  88. case '--help':
  89. console.group('使用手册')
  90. console.group('示例')
  91. console.log(`$i('--help') 查看帮助`)
  92. console.log(`$i("--version") 查看版本`)
  93. console.log(`$i("--info") 查看作者信息`)
  94. console.log(`$i("vue") 加载包`)
  95. console.groupEnd('示例')
  96. console.group('切换源')
  97. console.log(`$i.pkg('vue')`)
  98. console.log(`$i.esm('vue')`)
  99. console.log(`$i.cdn('vue')`)
  100. console.table([
  101. {
  102. cdn: '$import.cdn(packageName)',
  103. pkg: '$import.pkg(packageName)',
  104. esm: '$import.esm(packageName)'
  105. },
  106. {
  107. cdn: '使用CDNJS加载数据',
  108. pkg: '使用UNPKG加载数据(默认)',
  109. esm: '使用ESM加载数据'
  110. }
  111. ])
  112. console.groupEnd('切换源')
  113. console.groupEnd('使用手册')
  114. return false
  115. break
  116. case '--version':
  117. console.log('v1.0.1')
  118. return false
  119. break
  120. case '--info':
  121. console.log('作者: sunShineGo\n更新时间: 2022-12-26')
  122. return false
  123. break
  124.  
  125. default:
  126. return true
  127. break
  128. }
  129. }

QingJ © 2025

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