My jQuery Plugin

获取URL参数

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/35940/241249/My%20jQuery%20Plugin.js

  1. // ==UserScript==
  2. // @version 0.0.4
  3. // @modifvm 2018.01.05
  4. // @name My jQuery Plugin
  5. // @homepage https://greasyfork.org/zh-CN/scripts/35940
  6. // ==/UserScript==
  7.  
  8. (function ($) {
  9. $.getUrlParam = function(name, url, option) {//筛选参数,url 参数为数字时
  10. url = url ? url.replace(/^.+\?/,'') : location.search;
  11. //网址传递的参数提取,如果传入了url参数则使用传入的参数,否则使用当前页面的网址参数
  12. var reg = new RegExp("(?:^|&)(" + name + ")=([^&]*)(?:&|$)", "i"); //正则筛选参数
  13. var str = url.replace(/^\?/,'').match(reg);
  14.  
  15. if (str !== null) {
  16. switch(option) {
  17. case 'param':
  18. case 0:
  19. return unescape(str[0]); //所筛选的完整参数串
  20. case 'name':
  21. case 1:
  22. return unescape(str[1]); //所筛选的参数名
  23. case 'value':
  24. case 2:
  25. return unescape(str[2]); //所筛选的参数值
  26. default:
  27. return unescape(str[2]); //默认返回参数值
  28. }
  29. } else {
  30. return null;
  31. }
  32. }
  33. //设置新的参数
  34. $.setUrlParam = function(name, url, newVal){
  35. var checkParam=$.getUrlParam(name, url, 0);
  36. if(checkParam){
  37. return url.replace(checkParam, $.getUrlParam(name, url, 1)+"="+newVal);
  38. }
  39. }
  40. })(jQuery);