Url-Editor

编辑 url 用

As of 2020-05-10. See the latest version.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/402991/803589/Url-Editor.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

  1. // ==UserScript==
  2. // @name Url-Editor
  3. // @namespace https://greatest.deepsurf.us/users/390742
  4. // @updateURL https://github.com/fjqingyou/url_editor/raw/master/src/urlEditor.js
  5. // @version 1.0.4
  6. // @include *
  7. // @description 编辑 url 用
  8. // @author fjqingyou
  9. // @match http://*/*
  10. // @grant none
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14. /**
  15. * Url 编辑模块
  16. */
  17. UrlEditor = {};
  18.  
  19. /**
  20. * 获取 url 参数
  21. * @param {*} url
  22. * @param {*} variable
  23. * @returns 取得目标参数的值,不存在时返回 null
  24. */
  25. UrlEditor.getUrlParam = function(url, variable){
  26. var result = null;
  27. var nIndexQuestionMark = url.indexOf('?');
  28. if(nIndexQuestionMark){//如果存在参数
  29. var query = url.substring(nIndexQuestionMark + 1);
  30. var vars = query.split("&");
  31. for (var i = 0; i < vars.length; i++) {
  32. var pair = vars[i].split("=");
  33. if(pair[0] === variable){//如果找到目标了
  34. result = pair[1] || null;//取值,如果值不存在,返回null
  35. break;
  36. }
  37. }
  38. }
  39. return result;
  40. };
  41.  
  42. /**
  43. * 设置 url 参数
  44. * @param {*} url
  45. * @param {*} name
  46. * @param {*} value
  47. * @returns 返回变更后的 url
  48. */
  49. UrlEditor.setUrlParam = function(url, name, value){
  50. var result;
  51. var nIndexQuestionMark = url.indexOf('?');
  52. if(!nIndexQuestionMark){//如果目标url上面还未有这个属性
  53. result = url + '?' + name + '=' + value;
  54. }else{//如果url上面已经存在了这个参数
  55. var nIndex = nIndexQuestionMark + 1;
  56. result = url.substring(0, nIndex);
  57. var query = url.substring(nIndex);
  58.  
  59. var needAppend = true;
  60. var vars = query.split('&');
  61. for (var i = 0; i < vars.length; i++) {
  62. var pair = vars[i].split('=');
  63. var key = pair[0];
  64. var v = pair[1];
  65.  
  66. if(i > 0){
  67. result += "&";
  68. }
  69. result += key;
  70. result += '=';
  71. if(key !== name){//如果不是目标参数
  72. result += v;//保持原有值
  73. }else{//如果是目标参数
  74. result += value;//修改值
  75. needAppend = false;//标记为已经修改。不需要末尾填充了
  76. }
  77. }
  78.  
  79. if(needAppend){//如果需要末尾填充
  80. result += '&' + name + '=' + value;
  81. }
  82. }
  83. return result;
  84. };