微博ip属地显示助手

新浪微博显示用户ip属地

  1. // ==UserScript==
  2. // @name 微博ip属地显示助手
  3. // @name:zh 微博ip属地显示助手
  4. // @name:zh-CN 微博ip属地显示助手
  5. // @description 新浪微博显示用户ip属地
  6. // @description:zh 新浪微博显示用户ip属地
  7. // @description:zh-CN 新浪微博显示用户ip属地
  8. // @version 1.3
  9. // @author NiaoBlush
  10. // @license GPL
  11. // @namespace https://github.com/NiaoBlush/weibo-ip-location
  12. // @homepageURL https://github.com/NiaoBlush/weibo-ip-location
  13. // @supportURL https://github.com/NiaoBlush/weibo-ip-location/issues
  14. // @match https://weibo.com/*
  15. // @match https://m.weibo.cn/*
  16. // @require https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js
  17. // @grant GM.xmlHttpRequest
  18. // @connect weibo.com
  19. // ==/UserScript==
  20.  
  21.  
  22. (function () {
  23. "use strict";
  24.  
  25. function getRegion(uid) {
  26. return new Promise((resolve, reject) => {
  27. $.get(`https://weibo.com/ajax/profile/detail?uid=${uid}`, function (res) {
  28. if (res.data && res.data.ip_location) {
  29. const regionFull = res.data.ip_location;
  30. console.debug("[weibo-ip-location] info", uid, regionFull);
  31. const array = /IP属地:(.+)/.exec(regionFull);
  32. if (array != null) {
  33. resolve(array[1]);
  34. } else {
  35. resolve("")
  36. }
  37. } else {
  38. resolve("")
  39. }
  40. })
  41. })
  42. }
  43.  
  44. function getRegionGM(uid) {
  45. return new Promise(((resolve, reject) => {
  46. GM.xmlHttpRequest({
  47. url: `https://weibo.com/ajax/profile/detail?uid=${uid}`,
  48. method: "GET",
  49. onload: function (xhr) {
  50. const res = JSON.parse(xhr.responseText)
  51. if (res.data && res.data.ip_location) {
  52. const regionFull = res.data.ip_location;
  53. console.debug("[weibo-ip-location] info", uid, regionFull);
  54. const array = /IP属地:(.+)/.exec(regionFull);
  55. if (array != null) {
  56. resolve(array[1]);
  57. } else {
  58. resolve("")
  59. }
  60. } else {
  61. resolve("")
  62. }
  63. }
  64. });
  65. }))
  66. }
  67.  
  68. const district = ["北京", "天津", "河北", "山西", "内蒙古", "辽宁", "吉林", "黑龙江", "上海", "江苏", "浙江", "安徽", "福建", "江西", "山东", "河南", "湖北", "湖南", "广东", "广西", "海南", "重庆", "四川", "贵州", "云南", "西藏", "陕西", "甘肃", "青海", "宁夏", "新疆", "台湾", "中国香港", "澳门"];
  69.  
  70. const mark = ($obj, region) => {
  71. const markedClass = "weibo-ip-marked";
  72. if (!region || ($obj.hasClass(markedClass))) {
  73. return;
  74. }
  75. $obj.addClass(markedClass);
  76. const foreign = region && district.indexOf(region) === -1
  77.  
  78. let html;
  79. if (foreign) {
  80. html = `<span style="background-color: red;color: #FFF;margin-left: 5px;font-weight: bold;border-radius: 8px;padding: 2px 5px;">${region}</span>`;
  81. } else {
  82. html = `<span style="color: #00d0ff;margin-left: 5px;font-weight: normal;border-radius: 8px;padding: 2px 5px;">(${region})</span>`;
  83. }
  84. $obj.append(html);
  85. }
  86.  
  87. console.log("[weibo ip region] $.fn.jquery", $.fn.jquery);
  88.  
  89. const regionMap = {}
  90.  
  91. //v6
  92. $(".WB_main").bind("DOMNodeInserted", function (e) {
  93. const $e = $(e.target);
  94. if ($e.attr("id") === "v6_pl_content_homefeed") {
  95. $(".WB_main").unbind();
  96. console.log("$e.html()", $e.html());
  97. $e.bind("DOMNodeInserted", function (ev) {
  98. processList($(ev.target))
  99. })
  100. }
  101. })
  102.  
  103. //v7
  104. $("[class^='Home_feed']").bind("DOMNodeInserted", function (e) {
  105. const ele = $(e.target)
  106. processList(ele)
  107. })
  108.  
  109. function processList($ele) {
  110. const list = $ele.find("a[class^='ALink_default']:not([aria-label]),.WB_info>a[usercard]")
  111. list.each(async function () {
  112. const href = $(this).attr("href");
  113. const array = /\/u\/(\d+)/.exec(href)
  114. if (array != null) {
  115. const uid = array[1];
  116. let region = regionMap[uid]
  117. if (region === undefined) {
  118. region = await getRegion(uid);
  119. regionMap[uid] = region;
  120. }
  121. mark($(this), region)
  122. }
  123. })
  124. }
  125.  
  126. //mobile
  127. if (location.host === "m.weibo.cn") {
  128.  
  129. $("#app").bind("DOMNodeInserted", function (appE) {
  130. const appChild = $(appE.target)
  131. if (appChild.hasClass("main-wrap")) {
  132.  
  133. $("#app").unbind("DOMNodeInserted");
  134. appChild.bind("DOMNodeInserted", function (mainE) {
  135.  
  136. const mainChild = $(mainE.target)
  137. if (mainChild.is("div") && mainChild.attr("class") === undefined) {
  138. appChild.unbind("DOMNodeInserted");
  139. processMobileList(mainChild);
  140. $(".pannelwrap").bind("DOMNodeInserted", function (pE) {
  141.  
  142. processMobileList($(pE.target));
  143. })
  144. }
  145.  
  146. })
  147.  
  148. }
  149.  
  150. })
  151.  
  152. function processMobileList($ele) {
  153. const list = $ele.find(".weibo-top .m-text-box > a, .weibo-text > span > a:not([data-hide])")
  154. list.each(async function () {
  155. let $target = $(this);
  156.  
  157. const href = $target.attr("href");
  158. const array = /\/profile\/(\d+)/.exec(href);
  159. if ($(this).parent().hasClass("m-text-box")) {
  160. $target = $target.find("h3").first();
  161. }
  162. if (array != null) {
  163. const uid = array[1];
  164. let region = regionMap[uid]
  165. if (region === undefined) {
  166. region = await getRegionGM(uid);
  167. regionMap[uid] = region;
  168. }
  169. mark($target, region)
  170. }
  171. })
  172.  
  173. }
  174. }
  175.  
  176. })();
  177.  
  178.  

QingJ © 2025

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