Greasy Fork is available in English.

weibo following backup

backup weibo following list.

  1. // ==UserScript==
  2. // @name weibo following backup
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description backup weibo following list.
  6. // @author Raven1996
  7. // @match *://weibo.com/*
  8. // @match *://www.weibo.com/*
  9. // @grant GM_xmlhttpRequest
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. window.addEventListener("load", function(){
  14. setTimeout(setUI, 0);
  15. });
  16.  
  17. function setUI() {
  18. var setting = document.getElementsByClassName("gn_topmenulist_set")[0].children[0];
  19. var li = document.createElement('li');
  20. var button = document.createElement('a');
  21. button.innerText = "备份关注";
  22. button.onclick = getFollowStart;
  23. li.append(button);
  24. setting.insertBefore(li, setting.children[8]);
  25. }
  26.  
  27. function getFollowStart() {
  28. var urlBase = /[\w:\/.]+weibo.com\/p\/\d+/.exec(document.URL)
  29. || /[\w:\/.]+weibo.com\/[\w\d]+/.exec(document.URL);
  30. if (urlBase == null) return;
  31. urlBase = urlBase[0] + "/";
  32. var canceled = false;
  33. var numPage = 1, allPage = 0;
  34. var back = document.createElement('div');
  35. document.body.appendChild(back);
  36. back.style.cssText = "position:fixed; top:0; left:0; width:100%; height:100%; background:black; opacity:0.3; z-index:9999;"
  37. var layer = document.createElement('div');
  38. layer.className = "W_layer";
  39. layer.style.cssText = "width:240px; top:50%; left:50%; position:fixed; transform:translate(-50%, -50%);"
  40. layer.innerHTML = "<div class=\"content\" style=\"padding:16px; box-sizing:border-box;\">"
  41. + "<span class=\"txt\" style=\"display:inline-block; width:146px;\">获取关注中...</span>"
  42. + "<a class=\"W_btn_a\" href=\"javascript:void(0);\">取消</a></div>";
  43. layer.getElementsByTagName("a")[0].onclick = cancelThis;
  44. document.body.appendChild(layer);
  45. var followingList = "<html>\n<head><meta charset=\"UTF-8\"><title>Following List</title></head>\n<body>\n";
  46. GM_xmlhttpRequest({
  47. method: "GET",
  48. url: urlBase + "myfollow?t=1&cfs=&Pl_Official_RelationMyfollow__92_page=" + numPage,
  49. onload: function(response) {
  50. if (canceled) return;
  51. var pageHTML = getFollowHTML(response.responseText);
  52. if (pageHTML.getElementsByClassName("W_pages").length == 0) {
  53. cancelThis();
  54. return;
  55. }
  56. var children = pageHTML.getElementsByClassName("W_pages")[0].children;
  57. allPage = parseInt(children[children.length - 2].innerText);
  58. if (numPage <= allPage) {
  59. followingList += getFollowing(pageHTML);
  60. layer.getElementsByTagName("span")[0].innerText = "获取关注中...("+ Math.round(100.0*numPage/allPage) +"%)";
  61. if (numPage < allPage) {
  62. numPage++;
  63. GM_xmlhttpRequest({
  64. method: "GET",
  65. url: urlBase + "myfollow?t=1&cfs=&Pl_Official_RelationMyfollow__92_page=" + numPage,
  66. onload: nextPageFunction
  67. });
  68. }
  69. }
  70. }
  71. })
  72.  
  73. function cancelThis() {
  74. canceled = true;
  75. document.body.removeChild(back);
  76. document.body.removeChild(layer);
  77. return false;
  78. }
  79.  
  80. function nextPageFunction(response) {
  81. if (canceled) return;
  82. var pageHTML = getFollowHTML(response.responseText);
  83. followingList += getFollowing(pageHTML);
  84. layer.getElementsByTagName("span")[0].innerText = "获取关注中...("+ Math.round(100.0*numPage/allPage) +"%)";
  85. if (numPage < allPage){
  86. numPage++;
  87. GM_xmlhttpRequest({
  88. method: "GET",
  89. url: urlBase + "myfollow?t=1&cfs=&Pl_Official_RelationMyfollow__92_page=" + numPage,
  90. onload: nextPageFunction
  91. });
  92. }
  93. else if (numPage == allPage) {
  94. followingList += "</body>\n</html>";
  95. var blob = new Blob([followingList], {type: "text/plain"});
  96. var link = document.createElement("a");
  97. link.href = URL.createObjectURL(blob);
  98. link.download = 'Following List.html'
  99. link.click();
  100. cancelThis();
  101. }
  102. }
  103. }
  104.  
  105. function getFollowing(pageHTML) {
  106. var list = "";
  107. var menbers = pageHTML.getElementsByClassName('member_li');
  108. for (var index = 0; index < menbers.length; index++) {
  109. var text = menbers[index].getAttribute("action-data");
  110. text = text.split("&")
  111. var name, uid;
  112. for(var i = 0; i < text.length; i++) {
  113. var attr = text[i].substr(0, text[i].indexOf("="));
  114. if (attr == "uid") {
  115. uid = text[i].substr(text[i].indexOf("=") + 1);
  116. }
  117. else if (attr == "screen_name") {
  118. name = text[i].substr(text[i].indexOf("=") + 1);
  119. }
  120. }
  121. var userLink = "https://weibo.com/" + uid;
  122. list += "<p>"+ name + " <a href=\"" + userLink + "\" target=\"_blank\">" + userLink +"</a></p>\n";
  123. }
  124. return list;
  125. }
  126.  
  127. function getFollowHTML(originText) {
  128. var pageHTML = new DOMParser().parseFromString(originText, "text/html");
  129. var scripts = pageHTML.getElementsByTagName('script');
  130. var text;
  131. for (var index = 0; index < scripts.length; index++) {
  132. text = scripts[index].innerText;
  133. if (text.slice(27, 35) == "myFollow"){
  134. break;
  135. }
  136. }
  137. text = text.slice(text.indexOf("\"html\"") + 8, -3);
  138. text = text.replace(/\\r\\n/g, "\n");
  139. text = text.replace(/\\t/g, "\t");
  140. text = text.replace(/\\([\/\\"'])/g, "$1");
  141. return new DOMParser().parseFromString(text, "text/html");
  142. }
  143. })();