flutter packages site toolkit script

1. copy dependencies info cash as: "dio: ^1.2.3"; ~~2. open package page by search string~~

  1. // ==UserScript==
  2. // @name flutter packages site toolkit script
  3. // @license GPL-3.0-only
  4. // @namespace https://pub.dev
  5. // @icon https://pub.flutter-io.cn/favicon.ico?hash=nk4nss8c7444fg0chird9erqef2vkhb8
  6. // @version 1.0.0
  7. // @description 1. copy dependencies info cash as: "dio: ^1.2.3"; ~~2. open package page by search string~~
  8. // @author Sven
  9. // @match https://pub.dev/packages/*
  10. // @match https://pub.dev/packages?q=*
  11. // @match https://pub.dev
  12. // @match https://pub.flutter-io.cn/packages/*
  13. // @match https://pub.flutter-io.cn/packages?q=*
  14. // @match https://pub.flutter-io.cn
  15. // @run-at document-start
  16. // @grant none
  17. // ==/UserScript==
  18.  
  19. ;(function () {
  20. class Toolkit {
  21. static SHEETS = `
  22. :root {
  23. --toolkit-border: 1px solid #CCC;
  24. }
  25. #dependencies-info { display: flex; font-size: 14px; margin-bottom: 15px; }
  26. #dependencies-info > div { padding: 5px 8px; font-weight: bolder; cursor: pointer; }
  27. #dependencies-info .version {
  28. background-color: #e7f8ff;
  29. color: #666;
  30. }
  31. #dependencies-info .tips {
  32. background-color: #0175c2;
  33. color: #FFF;
  34. }
  35. #dependencies-info .tips:hover { background-color: #37a4ec; }
  36. `
  37. constructor() {
  38. window.addEventListener('DOMContentLoaded', evt => {
  39. this.appendSheets()
  40. this.showDependenciesInfo()
  41. })
  42. }
  43. appendSheets() {
  44. const sheet = document.createTextNode(Toolkit.SHEETS)
  45. const el = document.createElement('style')
  46. el.id = 'toolkit-sheets'
  47. el.appendChild(sheet)
  48. document.getElementsByTagName('head')[0].appendChild(el)
  49. }
  50. showDependenciesInfo() {
  51. const titleWrapper = document.querySelector('.detail-header')
  52. const titleDom = document.querySelector('.detail-header .title')
  53. const infoParts = titleDom.innerText.split(' ')
  54. const info = `${infoParts[0]}: ^${infoParts[1]}`
  55. const infoDom = this._getDependenciesInfoDom(info)
  56. titleWrapper.insertBefore(infoDom, document.querySelector('.detail-header .metadata'))
  57. }
  58. _getDependenciesInfoDom(info) {
  59. const infoDom = document.createElement('div')
  60. const versionDom = document.createElement('div')
  61. const tipsDom = document.createElement('div')
  62. versionDom.innerText = info
  63. versionDom.classList.add('version')
  64. tipsDom.innerText = location.host === 'pub.dev' ? '𝒄𝒍𝒊𝒄𝒌 𝒕𝒐 𝒄𝒐𝒑𝒚' : '点击复制'
  65. tipsDom.classList.add('tips')
  66. infoDom.setAttribute('id', 'dependencies-info')
  67. infoDom.appendChild(versionDom)
  68. infoDom.appendChild(tipsDom)
  69. infoDom.addEventListener('click', evt => {
  70. if (evt.target === infoDom) return
  71. this.clickToCopy(info)
  72. })
  73. return infoDom
  74. }
  75. clickToCopy(info) {
  76. const tempInput = document.createElement('input')
  77. tempInput.setAttribute('value', info)
  78. document.body.appendChild(tempInput)
  79. tempInput.select()
  80. const successfully = document.execCommand('copy')
  81. document.body.removeChild(tempInput)
  82. this.log(successfully ? 'copy successfully' : 'copy failed')
  83. }
  84. log(...args) {
  85. console.log('%c[pub.dev Toolkit] LOG: ', 'color:teal', ...args)
  86. }
  87. }
  88. window._$PubDevToolkit = new Toolkit()
  89. })();