v2ex user tag

v2ex用户标记

  1. // ==UserScript==
  2. // @name v2ex user tag
  3. // @namespace https://greasyfork.org/zh-CN/scripts/437891-v2ex-user-tag
  4. // @version 0.2
  5. // @description v2ex用户标记
  6. // @author yatzh
  7. // @match *://*.v2ex.com/*
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. // @grant GM_deleteValue
  11. // @license MIT
  12. // ==/UserScript==
  13. (function () {
  14. 'use strict';
  15. // store key | 加上版本号,方便后续更改存储格式
  16. const TAG_JSON_STR_STORE_KEY = 'plugin.user_tag.tag_json_str.v0.1';
  17. const TAG_STYLE = 'border: 1px solid #FF1744;color:#FF1744;border-radius:3px;padding:1px 3px;margin:1px 3px';
  18. const TAG_EDIT_CONTAINER_STYLE = 'background:#7e57c2; padding:5px; border-radius:10px';
  19. const TAG_EDIT_LABEL_STYLE = 'color:white;font-size:16px;margin:5px;';
  20. function getTagMap() {
  21. let tagJsonStr = GM_getValue(TAG_JSON_STR_STORE_KEY, "{}");
  22. return JSON.parse(tagJsonStr);
  23. // return {"zw1one" : "标签A,标签B", "Jooooooooo" : "", "Illusionary": "112"};
  24. }
  25. function saveTagMap(tagMap) {
  26. GM_setValue(TAG_JSON_STR_STORE_KEY, JSON.stringify(tagMap));
  27. }
  28. function tagUserName(username, tagMap) {
  29. if (tagMap[username.innerText]) {
  30. let tagValue = tagMap[username.innerText];
  31. let userTags = tagValue.split(',');
  32. for (let userTag of userTags) {
  33. let oneTag = document.createElement('span');
  34. oneTag.textContent = userTag;
  35. oneTag.setAttribute('style', TAG_STYLE);
  36. username.parentElement.insertBefore(oneTag, username)
  37. }
  38. }
  39. }
  40. // let url = document.URL;
  41. let path = location.pathname
  42. let tagMap = getTagMap();
  43. if (path == '/' || path.startsWith('/go/') || path.startsWith('/tag/')) {
  44. // 首页及类首页
  45. let home_list = document.getElementsByClassName('topic_info');
  46. let len = home_list.length;
  47. for (let i = 0; i < len; i++) {
  48. let username = path === '/' || path.startsWith('/tag/') ? home_list[i].children[2] : home_list[i].children[0];
  49. tagUserName(username, tagMap);
  50. }
  51. } else if (path.startsWith('/t/')) {
  52. // 帖子详情页
  53. // 主题
  54. let opUsername = document.getElementsByTagName('small')[0].children[0];
  55. tagUserName(opUsername, tagMap);
  56. // 回复
  57. let comments = document.getElementsByClassName('cell');
  58. for (let i = 0; i < comments.length; i++) {
  59. if (comments[i].id.substring(0, 2) != 'r_') {
  60. continue;
  61. }
  62. let username = comments[i].getElementsByTagName('strong')[0];
  63. tagUserName(username, tagMap);
  64. }
  65. } else if (path.startsWith('/member/')) {
  66. // 个人主页
  67. let username = document.getElementsByTagName('h1')[0];
  68. tagUserName(username, tagMap);
  69. // 标签编辑
  70. let usernameStr = username.innerText;
  71. let editContainer = document.createElement('div');
  72. editContainer.setAttribute('style', TAG_EDIT_CONTAINER_STYLE);
  73. let editLabel = document.createElement('span');
  74. editLabel.setAttribute('style', TAG_EDIT_LABEL_STYLE);
  75. editLabel.innerText = "标签编辑:";
  76. editContainer.appendChild(editLabel);
  77. let editTagContent = document.createElement('textarea');
  78. editTagContent.setAttribute('id', 'editTagContent');
  79. editTagContent.setAttribute('style', 'width:90%; height:50px');
  80. editTagContent.setAttribute('placeholder', '标签,英文逗号分割,如: sb,zz');
  81. editTagContent.value = tagMap[usernameStr] ? tagMap[usernameStr] : "";
  82. editContainer.appendChild(editTagContent);
  83. let saveBtn = document.createElement('input');
  84. saveBtn.setAttribute('type', 'button');
  85. saveBtn.setAttribute('id', 'saveBtn');
  86. saveBtn.setAttribute('value', '保存标签');
  87. saveBtn.setAttribute('class', 'normal button');
  88. editContainer.appendChild(saveBtn);
  89. document.getElementsByTagName('h1')[0].parentElement.appendChild(editContainer);
  90. document.getElementById('saveBtn').onclick = function() {
  91. let newTagContent = editTagContent.value;
  92. if (confirm('确认要保存?[' + usernameStr + ']的标签[' + newTagContent + ']?')) {
  93. tagMap[usernameStr] = newTagContent;
  94. saveTagMap(tagMap);
  95. location.reload();
  96. }
  97. };
  98. }
  99. })();