Apt-linker

Turns "apt-get install" lines into "apt://" apturl links

  1. // ==UserScript==
  2. // @name Apt-linker
  3. // @namespace http://ferk.gitlab.io/userscripts
  4. // @version 1.2
  5. // @description Turns "apt-get install" lines into "apt://" apturl links
  6. // @author Ferk
  7. // @grant none
  8. // @downloadUrl https://ferk.gitlab.io/userscripts/apt-linker.user.js
  9. // @updateUrl https://ferk.gitlab.io/userscripts/apt-linker.user.js
  10. // @icon https://ferk.gitlab.io/userscripts/apt-linker.user.png
  11. // @run-at document-end
  12. // @include *
  13. // ==/UserScript==
  14.  
  15. // Turns "apt-get install" lines into clickable apturl links for
  16. // installing Ubuntu and Debian GNU/Linux packages This extension is
  17. // intended to ease the life of Ubuntu/Debian users who browse the
  18. // Internet searching for software, tutorials, etc.
  19. //
  20. // Apt: urls are a way to install packages by using a protocol handler to
  21. // read the list of packages to install.
  22. //
  23. // This extension will turn lines of text describing commands for
  24. // installation into clickable links that allow you to install the
  25. // packages with just 2 clicks.
  26. //
  27. // More info about AptUrl can be found here:
  28. // https://help.ubuntu.com/community/AptURL
  29.  
  30. // Copyright (C) 2009 Fernando Carmona Varo
  31. //
  32. // This program is free software; you can redistribute it and/or
  33. // modify it under the terms of the GNU General Public License
  34. // as published by the Free Software Foundation; either version 2
  35. // of the License, or (at your option) any later version.
  36. //
  37. // This program is distributed in the hope that it will be useful,
  38. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. // GNU General Public License for more details.
  41. //
  42. // The GNU General Public License is available by visiting
  43. // http://www.gnu.org/copyleft/gpl.html
  44. // or by writing to
  45. // Free Software Foundation, Inc.
  46. // 51 Franklin Street, Fifth Floor
  47. // Boston, MA 02110-1301
  48. // USA
  49.  
  50.  
  51. (function() {
  52. /* == findAndReplace ==
  53. * No library or framework is required to use this function, it's entirely stand-alone. The function requires
  54. * two parameters, the third one is optional:
  55. *
  56. * * searchText - This can either be a string or a regular expression. Either way, it will eventually become a
  57. * RegExp object. So, if you wanted to search for the word "and" then that alone would not be appropriate - all
  58. * words that contain "and" would be matched so you need to use either the string, \\band\\b or the regular
  59. * expression, /\band\b/g to test for word boundaries. (remember the global flag)
  60. *
  61. * * replacement - This parameter will be directly passed to the String.replace function, so you can either have
  62. * a string replacement (using $1, $2, $3 etc. for backreferences) or a function.
  63. *
  64. * * searchNode - This parameter is mainly for internal usage but you can, if you so desire, specify the node
  65. * under which the search will take place. By default it's set to document.body.
  66. */
  67. function findAndReplace(searchText, replacement, searchNode) {
  68. if (!searchText || typeof replacement === 'undefined') {
  69. return;
  70. }
  71. let regex = typeof searchText === 'string' ? new RegExp(searchText, 'g') : searchText,
  72. childNodes = (searchNode || document.body).childNodes,
  73. cnLength = childNodes.length,
  74. excludes = 'html,head,style,title,link,meta,script,object,iframe,textarea';
  75. while (cnLength--) {
  76. let currentNode = childNodes[cnLength];
  77. if (currentNode.nodeType === 1 &&
  78. (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
  79. findAndReplace(searchText, replacement, currentNode);
  80. }
  81. if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) {
  82. continue;
  83. }
  84. let parent = currentNode.parentNode,
  85. html = currentNode.data.replace(regex, replacement),
  86. wrap = document.createElement('div'),
  87. frag = document.createDocumentFragment();
  88. wrap.innerHTML = html;
  89. while (wrap.firstChild) {
  90. frag.appendChild(wrap.firstChild);
  91. }
  92. parent.insertBefore(frag, currentNode);
  93. parent.removeChild(currentNode);
  94. }
  95. }
  96.  
  97.  
  98. findAndReplace('\\b(sudo )?(apt(-get)?|aptitude) install (\\w|-| |(\\.\\d))*\\b',
  99. function(aptline) {
  100. let pkgs= aptline.replace(/^.*install /,'').replace(/\s+/g,',');
  101. return '<a href=\"apt://'+pkgs+'\">'+aptline+'</a>';
  102. });
  103.  
  104. findAndReplace('\\bapt:(//)?[\\w,-]+',
  105. function(apturl) {
  106. return '<a href='+apturl+'>'+apturl+'</a>';
  107. });
  108. })();

QingJ © 2025

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