Greasy Fork 支持简体中文。

Reddit - Quick RES user tagging

Quickly tag multiple users with the same tag and open the tag popup using the keyboard

  1. // ==UserScript==
  2. // @name Reddit - Quick RES user tagging
  3. // @description Quickly tag multiple users with the same tag and open the tag popup using the keyboard
  4. // @author James Skinner <spiralx@gmail.com> (http://github.com/spiralx)
  5. // @namespace http://spiralx.org/
  6. // @version 2.2.0
  7. // @license MIT
  8. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAaxSURBVHhe7ZplqC1VGIav3d2t2FjY3QEG+EOxE1sMUBDzl60omOgFC+uHYmIXiih2d2N3dz7PuXvp55xZM7P32efefWBeeJgdE2vNrPXVmnGtWrVq1apVq1ZjUFPDQfAQfAN/wadwPWwOY1pTwuww09C34VoEnoG/K7gOpocxozngJHgV/oDUkR/gMTgbNoZ54G2Inc1xG0wOA6+NwOFb1okivxS+fwmnw35wCfwO8f99YKC1JvwEsdFNeRcWgqjN4DdI+7wMA6tp4E2InfoQToQ9YX9w6D8HcZ/ETlCmiyHutyAMpBy2saGPwixQpuXgToj7Lw5lctjH/daAUk1qA7F3Z6v+BJ/6t0PfhusluHXCx3+1RGdbVPH37zvbgdKsYKfTU7oH6rQ2xCerd5gOouz815D2+Q6mgoGTLi125mio02SgUYvHPQu7w4ZwJHwO8f+LYCC1L8SGbgtNtCnEkVOFrnVeyGpS2oC5OtskG9tE98GB4E2o0hewNXwy9G1A5Hx1jq4L10B8WnvAUtA0fF0PnoB4DjGKvBYauT7n1GjJOH4DsLOrgG5sAai7pp34CLT6T8HDYMJjSFymZWFl0H1+Bu7fdDR1JS2pSUqVZoC94C74FYpPp1c81x3guWeEnMwQjQ26GUlZaR92hftBP2pDTDnfh8thdUiaE06F6H5GC1Pfs2B+SNoCvEExT3AqPALetCmgVLnhOB+YTjp8c/IiV8MbcATkIjilwXodXg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aocX/fOFkJ+irzg2LRwXQ5t0bvUzkY9MPxmG4w+nQ65EaqiyBGh/EY21iZ8o5Eh0DxJph+Hga50phTaCvQDrwD8dginkujZ1a5NOTkOQ+FYupr22zLqMoiY9myl+v2ls6qFjOUVRpfWdGG+HRdFtdVLQp1Ftv/t4MXoXh97dFEeyNM9/QeFBshFh182aGX+D8nCy1HgatQZdd0ua6Jm+yrXMoaD7mXFRyOxgKu7DgFKl9UKMjM0bV9j/UcxWkXr3EpNE2ShqmJG6yTrsaGxgWUnFw81Q6Yoqa1QTti+d3Ywo67NtikQ9qKY2FEoW4/pY+24trrS49NsErlNXy5cmDlk/RtD9cY+7FcbmXYFSmNr9Our+rHFKiSHsFFTSM8t0Z7GjRXaQ1d0zqeXsWOuv6gQTPc1tLr4w2o+hLNtWrVqlWrVq1a/adx4/4BlQokldY0pQAAAAAASUVORK5CYII=
  9. // @supportURL https://greasyfork.org/en/scripts/370256-reddit-quick-user-tagging/feedback
  10. // @match *://*.reddit.com/r/*
  11. // @match *://*.reddit.com/user/*
  12. // @grant none
  13. // @run-at document-end
  14. // @require https://unpkg.com/jquery@3/dist/jquery.min.js
  15. // @require https://greasyfork.org/scripts/389748-console-message-v2/code/console-message-v2.js?version=730537
  16. // ==/UserScript==
  17.  
  18. /* jshint asi: true, esnext: true, laxbreak: true */
  19. /* global jQuery, message */
  20.  
  21. /**
  22.  
  23. ==== 2.2.0 (2022.07.24) ====
  24. * Change name and description
  25. * Add Q keyboard shortcut to open tag dialog
  26. * Removed references to unused Watcher object
  27. * Tidy up some code
  28.  
  29. ==== 2.1.2 (2022.07.01) ====
  30. * Update icon and add license metadata field
  31.  
  32. ==== 2.1.1 (2022.04.16) ====
  33. * Clicking 'Clear tag' closes tag popup
  34.  
  35. ==== 2.1.0 (2021.11.05) ====
  36. * Fix when ConsoleMessage not available
  37.  
  38. ==== 2.0.5 (2021.06.08) ====
  39. * Update icons
  40.  
  41. ==== 2.0.4 (2021.06.02) ====
  42. * Set width of tag dialog to 800px
  43.  
  44. ==== 2.0.0 (2019.09.03) ====
  45. * Rename script and update version to 2.0.0
  46.  
  47. ==== 1.0.0 (2018.10.09) ====
  48. * Rewrite code base
  49. * Set previous tag on preset tag clicked
  50.  
  51. ==== 0.9.0 (2018.10.09) ====
  52. * Made tag modal wider and text smaller to accomodate more preset tags
  53. * Added clear tag link to the right of the colour drop-down
  54.  
  55. ==== 0.8.0 (2018.07.13) ====
  56. * Changed console.message require to use GreasyFork
  57.  
  58. ==== 0.7.0 (2018.02.13) ====
  59. * Changed the rendering of the tag preview to match how RES does it
  60. * Moved output to all use console.message
  61.  
  62. ==== 0.6.0 (2018.02.11) ====
  63. * Use unpkg.com for jQuery
  64. * Add console.message for logging
  65. * Use localstorage to save tags
  66. * Use new Tag class to store tag info
  67. * Updated ID for text field in tag popup
  68.  
  69. ==== 0.5.1 (2018.02.11) ====
  70. * Update icons to match other Reddit script
  71.  
  72. ==== 0.5.0 (26.08.2017) ====
  73. * Change to simple use of GM_getValue and GM_setValue for storage
  74.  
  75. ==== 0.4.0 (21.08.2017) ====
  76. * Update all other tags correctly when changing a user's tag
  77. * Handle removing all other tags when clearing a user's tag
  78.  
  79. ==== 0.3.0 (13.07.2017) ====
  80.  
  81. * Checks tag link to see if tag set, always overwrites if not
  82. * Updates other tags for same user on current page
  83.  
  84. ==== 0.2.1 (27.06.2017) ====
  85. * Changed timeout of field set function to 250ms
  86.  
  87. ==== 0.2.0 (31.05.2017) ====
  88. * Updated jQuery to v3.2.1
  89. * Added timeout before overriding tag/colour fields
  90. * Update preview when setting tag/colour
  91.  
  92. */
  93.  
  94. ; (($, message) => {
  95.  
  96. const STYLES = {
  97. func: { color: '#c41', 'font-weight': 'bold' },
  98. attr: { color: '#1a2', 'font-weight': 'bold' },
  99. value: { color: '#05f' },
  100. punc: { 'font-weight': 'bold' },
  101. // comment: { color: 'c1007f' },
  102. // error: { color: '#f4f', 'font-weight': 'bold' },
  103. // link: { color: '#05f', 'text-decoration': 'underline' },
  104. }
  105.  
  106. // --------------------------------------------------------------------------
  107.  
  108. if (message) {
  109. message().extend({
  110. tag({ text, colour }) {
  111. return this.text(text, {
  112. fontSize: '0.9em',
  113. padding: '0 4px',
  114. border: 'solid 1px rgb(199, 199, 199)',
  115. borderRadius: 3,
  116. ...getStyleForColour(colour)
  117. })
  118. }
  119. })
  120. }
  121.  
  122. // --------------------------------------------------------------------------
  123.  
  124. const TAG_STORAGE_KEY = 'resPreviousUserTag'
  125.  
  126. const TAG_DIALOG_OPEN_TIMEOUT = 200
  127.  
  128. const BACKGROUND_TO_TEXT_COLOUR_MAP = {
  129. none: 'inherit',
  130. aqua: 'black',
  131. lime: 'black',
  132. pink: 'black',
  133. silver: 'black',
  134. white: 'black',
  135. yellow: 'black'
  136. }
  137.  
  138. const IGNORE_TAGS = new Set([ 'A', 'BUTTON', 'INPUT', 'TEXTAREA' ])
  139.  
  140. // --------------------------------------------------------------------
  141.  
  142. const CLEAR_TAG_LINK = `
  143. <a href="javascript:void(0)"
  144. title="Clear tag">
  145. clear tag
  146. </a>
  147. `
  148.  
  149. const NO_PREVIEW_TAG = `
  150. <span class="RESUserTag">
  151. <a href="javascript:void 0"
  152. title="Set a tag"
  153. class="RESUserTagImage userTagLink truncateTag"
  154. >&nbsp;</a>
  155. </span>
  156. `
  157.  
  158. const QUICK_REPEAT_TAG = `
  159. <span class="RESUserTag" style="filter: hue-rotate(90deg) saturate(0.5);">
  160. <a href="javascript:void 0"
  161. title="Repeat previous tag"
  162. class="RESUserTagImage userTagLink truncateTag"
  163. >&nbsp;</a>
  164. </span>
  165. `
  166.  
  167. // --------------------------------------------------------------------------
  168.  
  169. function getStyleForColour (colour) {
  170. return {
  171. color: BACKGROUND_TO_TEXT_COLOUR_MAP[ colour ] || 'white',
  172. backgroundColor: colour === 'none'
  173. ? 'transparent'
  174. : colour
  175. }
  176. }
  177.  
  178. // --------------------------------------------------------------------------
  179.  
  180. const getPreviewForTag = ({ text, colour }) => {
  181. const { color, backgroundColor } = getStyleForColour(colour)
  182.  
  183. return `
  184. <span class="RESUserTag">
  185. <a href="javascript:void 0"
  186. title="${text}"
  187. class="userTagLink hasTag truncateTag"
  188. style="color: ${color}; background-color: ${backgroundColor};"
  189. >${text}</a>
  190. </span>
  191. `
  192. }
  193.  
  194. // --------------------------------------------------------------------------
  195.  
  196. const EMPTY_TAG = Object.freeze({
  197. text: '',
  198. colour: 'none'
  199. })
  200.  
  201. const isEmptyTag = ({ text, colour } = EMPTY_TAG) => text === '' && colour === 'none'
  202.  
  203. const areEqualTags = (a, b) => a.text === b.text && a.colour === b.colour
  204.  
  205. // --------------------------------------------------------------------------
  206.  
  207. let previousUserTag = EMPTY_TAG
  208. let ctx = null
  209.  
  210. if (localStorage[ TAG_STORAGE_KEY ]) {
  211. previousUserTag = JSON.parse(localStorage[ TAG_STORAGE_KEY ])
  212. }
  213.  
  214. if (message) {
  215. let msg = message()
  216. .text('reddit-save-res-tag.init', STYLES.func)
  217. .text(': ', STYLES.punc)
  218.  
  219. if (!isEmptyTag(previousUserTag)) {
  220. msg = msg
  221. .text('previousUserTag', STYLES.attr)
  222. .text(' = ', STYLES.punc)
  223. .tag(previousUserTag)
  224. } else {
  225. msg = msg.text('No previous tag found')
  226. }
  227.  
  228. msg.print()
  229. } else if (previousUserTag !== EMPTY_TAG) {
  230. console.log(`previousUserTag = %o`, previousUserTag)
  231. }
  232.  
  233. // --------------------------------------------------------------------------
  234.  
  235. class TagContext {
  236. constructor ($thingElem) {
  237. this.$thingElem = $thingElem
  238.  
  239. this.authorId = $thingElem.data('author-fullname')
  240.  
  241. this.$dialog = $('.userTagger-dialog-head').closest('.RESHover')
  242.  
  243. this.user = $('.RESHover .RESHoverTitle .res-icon + span').text() || null
  244.  
  245. this.$textField = $('#userTaggerText')
  246. this.$colourField = $('#userTaggerColor')
  247. this.$previewElem = $('#userTaggerPreview')
  248. this.$presetTags = $('#userTaggerPresetTags')
  249. }
  250.  
  251. clearFields () {
  252. this.$textField.val('').focus()
  253. this.$colourField.val('none')
  254. this.$previewElem.html(NO_PREVIEW_TAG)
  255. }
  256.  
  257. setFields ({ text, colour }) {
  258. this.$textField.val(text)
  259. this.$colourField.val(colour)
  260. this.$previewElem.html(getPreviewForTag({ text, colour }))
  261. }
  262.  
  263. getTag () {
  264. return {
  265. text: this.$textField.val().trim(),
  266. colour: this.$colourField.val()
  267. }
  268. }
  269. }
  270.  
  271. // --------------------------------------------------------------------------
  272.  
  273. function onTagSelected (tag) {
  274. // console.info(`onTagSelected: tag = %o, ctx = %o, prevTag = %o`, tag, ctx, previousUserTag)
  275.  
  276. if (message) {
  277. message()
  278. .text('onTagSelected', STYLES.func)
  279. .text('(', STYLES.punc)
  280. .text('tag', STYLES.attr)
  281. .text(': ', STYLES.punc)
  282. .tag(tag)
  283. .text('): ', STYLES.punc)
  284. .text('previousUserTag', STYLES.attr)
  285. .text(' = ', STYLES.punc)
  286. .tag(previousUserTag)
  287. .text(', ', STYLES.punc)
  288. .text('ctx', STYLES.attr)
  289. .text(' = ', STYLES.punc)
  290. .object(ctx)
  291. .print()
  292. }
  293.  
  294. const $tagLinks = $(`.id-${ctx.authorId}`)
  295. .next()
  296. .children(0)
  297.  
  298. if (isEmptyTag(tag)) {
  299. $tagLinks
  300. .html('&nbsp;')
  301. .css('background-color', 'transparent')
  302. .removeClass('hasTag')
  303. .addClass('RESUserTagImage')
  304. } else if (!areEqualTags(tag, previousUserTag)) {
  305. previousUserTag = tag
  306. localStorage[TAG_STORAGE_KEY] = JSON.stringify(previousUserTag)
  307.  
  308. $tagLinks
  309. .text(tag.text)
  310. .css(getStyleForColour(tag.colour))
  311. .addClass('hasTag')
  312. .removeClass('RESUserTagImage')
  313. }
  314.  
  315. ctx = null
  316. }
  317.  
  318. // --------------------------------------------------------------------------
  319.  
  320. function onTagClicked ($tagLink) {
  321. const $thing = $tagLink.closest('.thing')
  322.  
  323. // console.info(`a.userTagLink.click`, this, $thing)
  324.  
  325. setTimeout(() => {
  326. ctx = new TagContext($thing)
  327. ctx.$dialog.width(800)
  328.  
  329. const authorHasTag = $tagLink.hasClass('hasTag')
  330.  
  331. if (!authorHasTag && !isEmptyTag(previousUserTag)) {
  332. ctx.setFields(previousUserTag)
  333. }
  334.  
  335. $(CLEAR_TAG_LINK)
  336. .click(function () {
  337. ctx.clearFields()
  338. $('#userTaggerSave').click()
  339. return false
  340. })
  341. .css({ marginLeft: 'auto' })
  342. .insertAfter(ctx.$colourField)
  343.  
  344. const $srcField = ctx.$previewElem.parent().next()
  345.  
  346. ctx.$presetTags
  347. .one('click.resrem', '.userTagLink', function () {
  348. const $clicked = $(this)
  349. const clickedTag = {
  350. text: $clicked.text(),
  351. colour: $clicked.css('background-color')
  352. }
  353.  
  354. // console.info(`preset .userTagLink.click`, this, clickedTag, ctx)
  355. onTagSelected(clickedTag)
  356. })
  357. .parent()
  358. .insertBefore($srcField)
  359.  
  360. $srcField.next().css({ float: 'left', width: '50%' })
  361. }, TAG_DIALOG_OPEN_TIMEOUT)
  362. }
  363.  
  364. // --------------------------------------------------------------------------
  365.  
  366. $('body')
  367. .on('click.resrem', '.RESUserTag', function () {
  368. const $tagLink = $(this).children('a.userTagLink')
  369. onTagClicked($tagLink)
  370. })
  371. .on('click.resrem', '#userTaggerSave', function () {
  372. if (ctx) {
  373. onTagSelected(ctx.getTag())
  374. }
  375. })
  376. .on('keypress.resrem', event => {
  377. if (ctx || event.key !== 'q' || IGNORE_TAGS.has(event.target.tagName)) {
  378. return
  379. }
  380.  
  381. $('div.thing.comment.noncollapsed.res-selected .entry.res-selected .RESUserTag').click()
  382.  
  383. return false
  384. })
  385.  
  386. // --------------------------------------------------------------------------
  387.  
  388. /*
  389. <div class="RESHover RESHoverInfoCard RESDialogSmall" style="top: 387.918px; left: 215.767px; width: 350px; display: block; opacity: 1;">
  390. <h3 class="RESHoverTitle" data-hover-element="0">
  391. <div>
  392. <span class="res-icon"></span>&nbsp;<span>Plan-Six</span>
  393. </div>
  394. </h3>
  395.  
  396. <div class="RESCloseButton">x</div>
  397.  
  398. <div class="RESHoverBody RESDialogContents" data-hover-element="1">
  399. <form id="userTaggerToolTip">
  400. <div class="fieldPair">
  401. <label class="fieldPair-label" for="userTaggerText">Text</label>
  402.  
  403. <input class="fieldPair-text" type="text" id="userTaggerText">
  404. </div>
  405.  
  406. <div class="fieldPair">
  407. <label class="fieldPair-label" for="userTaggerColor">Color</label>
  408.  
  409. <select id="userTaggerColor">
  410. <option style="color: inherit; background-color: none" value="none">none</option>
  411. <option style="color: black; background-color: aqua" value="aqua">aqua</option>
  412. <option style="color: white; background-color: black" value="black">black</option>
  413. <option style="color: white; background-color: blue" value="blue">blue</option>
  414. <option style="color: white; background-color: cornflowerblue" value="cornflowerblue">cornflowerblue</option>
  415. <option style="color: white; background-color: fuchsia" value="fuchsia">fuchsia</option>
  416. <option style="color: white; background-color: gray" value="gray">gray</option>
  417. <option style="color: white; background-color: green" value="green">green</option>
  418. <option style="color: black; background-color: lime" value="lime">lime</option>
  419. <option style="color: white; background-color: maroon" value="maroon">maroon</option>
  420. <option style="color: white; background-color: navy" value="navy">navy</option>
  421. <option style="color: white; background-color: olive" value="olive">olive</option>
  422. <option style="color: white; background-color: orange" value="orange">orange</option>
  423. <option style="color: white; background-color: orangered" value="orangered">orangered</option>
  424. <option style="color: black; background-color: pink" value="pink">pink</option>
  425. <option style="color: white; background-color: purple" value="purple">purple</option>
  426. <option style="color: white; background-color: red" value="red">red</option>
  427. <option style="color: black; background-color: silver" value="silver">silver</option>
  428. <option style="color: white; background-color: teal" value="teal">teal</option>
  429. <option style="color: black; background-color: white" value="white">white</option>
  430. <option style="color: black; background-color: yellow" value="yellow">yellow</option>
  431. </select>
  432. </div>
  433.  
  434. <div class="fieldPair">
  435. <label class="fieldPair-label" for="userTaggerPreview">Preview</label>
  436.  
  437. <span id="userTaggerPreview" style="color: white; background-color: olive;">
  438. <span class="RESUserTag">
  439. <a class="userTagLink hasTag truncateTag" style="background-color: olive; color: white !important;" title="Sexist" href="javascript:void 0">Sexist</a>
  440. </span>
  441. </span>
  442. </div>
  443. <a class="userTagLink hasTag truncateTag" style="background-color: olive; color: white !important;" title="Feminist" href="javascript:void 0">Feminist</a>
  444.  
  445. <div class="fieldPair res-usertag-ignore">
  446. <label class="fieldPair-label" for="userTaggerIgnore">Ignore</label>
  447.  
  448. <div id="userTaggerIgnoreContainer" class="toggleButton ">
  449. <span class="toggleThumb"></span>
  450. <div class="toggleLabel res-icon" data-enabled-text="" data-disabled-text=""></div>
  451. <input id="userTaggerIgnore" name="userTaggerIgnore" type="checkbox">
  452. </div>
  453.  
  454. <a class="gearIcon" href="#res:settings/userTagger/hardIgnore" title="RES Settings > User Tagger > hardIgnore"> configure </a>
  455. </div>
  456.  
  457. <div class="fieldPair">
  458. <label class="fieldPair-label" for="userTaggerLink">
  459. <span class="userTaggerOpenLink">
  460. <a title="open link" href="javascript:void 0">Source URL</a>
  461. </span>
  462. </label>
  463.  
  464. <input class="fieldPair-text" type="text" id="userTaggerLink" value="https://www.reddit.com/r/giantbomb/comments/7x251d/all_systems_goku_02/du5fpwr/">
  465. </div>
  466.  
  467. <div class="fieldPair">
  468. <label class="fieldPair-label" for="userTaggerVotesUp" title="Upvotes you have given this redditor">Upvotes</label>
  469.  
  470. <input type="number" style="width: 50px;" id="userTaggerVotesUp" value="0">
  471. </div>
  472.  
  473. <div class="fieldPair">
  474. <label class="fieldPair-label" for="userTaggerVotesDown" title="Downvotes you have given this redditor">Downvotes</label>
  475.  
  476. <input type="number" style="width: 50px;" id="userTaggerVotesDown" value="0">
  477. </div>
  478.  
  479. <div class="res-usertagger-footer">
  480. <a href="/r/dashboard#userTaggerContents" target="_blank" rel="noopener noreferer">View tagged users</a>
  481.  
  482. <input type="submit" id="userTaggerSave" value="✓ save tag">
  483. </div>
  484. </form>
  485. </div>
  486. </div>
  487.  
  488. */
  489.  
  490. })(jQuery, typeof message !== 'undefined' ? message : null)
  491.  
  492. jQuery.noConflict(true)