TJ User Tagging

3/29/2020, 5:46:18 PM

  1. // ==UserScript==
  2. // @name TJ User Tagging
  3. // @namespace Violentmonkey Scripts
  4. // @match https://tjournal.ru/*
  5. // @grant none
  6. // @version 1.11
  7. // @license CC0
  8. // @author -
  9. // @description 3/29/2020, 5:46:18 PM
  10. // ==/UserScript==
  11.  
  12. function setTag(userId) {
  13. let tags = JSON.parse(localStorage.userTags || '{}');
  14. let oldColor = '#' + ((tags[userId] ? tags[userId].split('#')[1] : null) || '888888');
  15. let newTag = prompt('Тег, или тег#цвет:', tags[userId] ? tags[userId].split('#')[0] : '');
  16. if (newTag == null) return;
  17. localStorage.userTags = JSON.stringify(Object.assign(tags, { [userId]: newTag }), (k, v) => v ? v : void 0);
  18. tagsUpdate();
  19. if (newTag && newTag.indexOf('#') === -1){
  20. with (document.body.appendChild(document.createElement('input'))){
  21. type = 'color';
  22. value = oldColor;
  23. style.display = 'none';
  24. onchange = function(){
  25. localStorage.userTags = JSON.stringify(Object.assign(tags, { [userId]: newTag + this.value }), (k, v) => v ? v : void 0);
  26. tagsUpdate();
  27. document.body.removeChild(this);
  28. };
  29. click();
  30. }
  31. }
  32. }
  33.  
  34. function addTagButton() {
  35. let target = document.querySelector('.etc_control[data-subsite-url]');
  36. if (target && !document.querySelector('._tg')) {
  37. with (target.insertAdjacentElement('afterend', document.createElement('div'))) {
  38. onclick = () => /\/(\d+)-/.test(location.href) && setTag(RegExp.$1);
  39. className = '_tg ui-button ui-button--5 l-ml-12 lm-ml-0 lm-mr-12';
  40. innerHTML = '<span></span>'
  41. }
  42. }
  43. setTimeout(addTagButton, 500);
  44. }
  45.  
  46. function isColorDark(color) {
  47. return !color || (color.length == 6 ? parseInt(color.substr(2, 2), 16) : parseInt(color[1], 16) * 16) < 200;
  48. }
  49.  
  50. function tagsUpdate() {
  51. let tags = JSON.parse(localStorage.userTags || '{}');
  52. (window._tgStyle || (window._tgStyle = document.body.appendChild(document.createElement('style')))).innerHTML = `
  53. .user_name:after, .content-header-author:after, .vote__users__item .vote__users__item__name:after, .live__item__user:after {
  54. display:inline-block; padding:2px 4px 3px 4px; margin:-2px 4px -3px 4px; border-radius:2px; font-weight:normal; font-size:small;
  55. }
  56. .vote__users__item .vote__users__item__name:after {
  57. padding:0 4px 0 4px; margin:-2px 4px -3px 4px;
  58. }
  59. .etc_control[data-subsite-url] + ._tg {
  60. content:"Тег"; height:34px; line-height:34px; font-size:15px; padding:0 20px; border-radius:4px;
  61. display:inline-block; font-weight:normal; margin-left:8px; cursor:pointer;
  62. }
  63. .etc_control[data-subsite-url] + ._tg span:after {
  64. content:"Тег";
  65. }
  66. ` + Object.keys(tags).filter(x => tags[x]).map(x =>
  67. `a[href*="/${x}-"] .user_name:after,
  68. .vote__users__item[href*="/${x}-"] .vote__users__item__name:after,
  69. .content-header-author[href*="/${x}-"]:after,
  70. .live__item__user[href*="/${x}-"]:after,
  71. .etc_control[data-subsite-url*="/${x}-"] + ._tg,
  72. .etc_control[data-subsite-url*="/${x}-"] + ._tg span:after {
  73. content: "${tags[x].split('#')[0]}";
  74. background: #${tags[x].split('#')[1] || '888'} !important; color: #${isColorDark(tags[x].split('#')[1]) ? 'fff' : '000'} !important;
  75. }`).join('\n');
  76. }
  77.  
  78. addTagButton();
  79. tagsUpdate();