即刻网页版自赞提示

当在即刻网页版给自己的动态点赞时,给你弹个 😏表情

  1. // ==UserScript==
  2. // @name 即刻网页版自赞提示
  3. // @namespace jike-web-like-myself
  4. // @version 0.1
  5. // @description 当在即刻网页版给自己的动态点赞时,给你弹个 😏表情
  6. // @author viko16 (即刻@糯米鸡)
  7. // @match https://web.okjike.com/*
  8. // @homepage https://github.com/viko16/jike-web-like-myself
  9. // @license The MIT License (MIT); http://opensource.org/licenses/MIT
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. /**
  16. * 1. 获取当前用户是谁
  17. * 2. 监听点赞的点击
  18. * 3. 判断这个赞的作者
  19. * 4. 一致就 😏
  20. */
  21.  
  22. // 放置 toast 元素
  23. const toastEl = document.createElement('div');
  24. toastEl.innerText = '😏';
  25. Object.assign(toastEl.style, {
  26. position: 'fixed',
  27. right: '30px',
  28. top: '80px',
  29. fontSize: '3em',
  30. visibility: 'hidden'
  31. });
  32. document.body.appendChild(toastEl);
  33.  
  34. getUserNick().then(userNick => {
  35. document.addEventListener('click', event => {
  36. const FLAG = 'symbol symbol-dig';
  37. const target = event.target;
  38.  
  39. try {
  40. // 定位到赞上面的 a 标签
  41. let a;
  42. if (target.classList.toString() === FLAG) {
  43. a = target.parentElement;
  44. } else if (target.nodeName === 'A' && target.firstElementChild && target.firstElementChild.classList.toString() === FLAG) {
  45. a = target;
  46. } else {
  47. return;
  48. }
  49.  
  50. // 没想到什么好办法,先数父节点吧..
  51. const author = a.parentElement.parentElement.parentElement.parentElement.querySelector('.user-activity-header-main .user-name a').innerText;
  52. // console.log(`给 ${author} 点赞了`);
  53. if (userNick === author) showEmoji();
  54. } catch (error) {
  55. // console.error('出错了呀');
  56. }
  57. });
  58. });
  59.  
  60. function showEmoji() {
  61. console.log('😏');
  62. toastEl.style.visibility = 'visible';
  63. setTimeout(() => {
  64. toastEl.style.visibility = 'hidden';
  65. }, 1000);
  66. }
  67.  
  68. function getUserNick() {
  69. return fetch('https://app.jike.ruguoapp.com/1.0/users/profile', {
  70. headers: {
  71. platform: 'web',
  72. 'App-Version': '4.1.0',
  73. 'x-jike-access-token': localStorage.getItem('access-token'),
  74. },
  75. })
  76. .then(res => res.json())
  77. .then(result => result.user.screenName);
  78. }
  79.  
  80. })();