让微博按正确的时间线排序

自动切换到最新微博,恢复正确的时间线

  1. // ==UserScript==
  2. // @name 让微博按正确的时间线排序
  3. // @namespace https://hehome.xyz/
  4. // @version 0.2.6
  5. // @icon https://weibo.com/favicon.ico
  6. // @description 自动切换到最新微博,恢复正确的时间线
  7. // @author hemengyang
  8. // @match https://weibo.com/*
  9. // @match https://www.weibo.com/*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15. // 判断是否是新版微博
  16. var isNewVersion = document.querySelector('a.gn_name') === null;
  17.  
  18. if (isNewVersion) {
  19. // 新版微博
  20. // 切换到最新微博
  21. function switchToLatest() {
  22. // 侧边栏的最新微博按钮
  23. var latestButton = document.querySelector('div[title="最新微博"]');
  24. if (latestButton === null) {
  25. // 无法找到最新微博的按钮,且不在主页时,
  26. // 会尝试重新加载网页
  27. if (window.location.pathname !== '/') {
  28. window.location = '/';
  29. }
  30. } else {
  31. latestButton.click();
  32. }
  33. }
  34. // 首页按钮
  35. var homeButton = document.querySelector('div[aria-label="首页"]');
  36. if (homeButton !== null) {
  37. // 通过 div[aria-label="首页"] 获取元素的父元素也能单击回到全部关注
  38. // 需要替换那个元素,否则单击主页按钮边缘会回到全部关注而不是最新微博
  39. homeButton = homeButton.parentElement
  40. // 替换首页按钮点击事件
  41. homeButton.addEventListener('click', (e) => { switchToLatest(); e.stopPropagation(); e.preventDefault(); }, true);
  42. }
  43. // 左上角的图标
  44. var homeIcon = document.querySelector('a[aria-label="Weibo"]');
  45. if (homeIcon !== null) {
  46. // 替换左上角的微博图标点击事件
  47. homeIcon.addEventListener('click', (e) => { switchToLatest(); e.stopPropagation(); e.preventDefault(); }, true);
  48. }
  49. // 每隔 500 毫秒检查一下
  50. // 如果在微博主页自动切换到最新微博
  51. var switchInterval = setInterval(function () {
  52. if (window.location.pathname === '/') {
  53. switchToLatest();
  54. } else {
  55. clearInterval(switchInterval);
  56. }
  57. }, 500);
  58. }
  59. else {
  60. // 老版微博
  61. var userId = document.querySelector('a.gn_name').href.split('/')[3];
  62. var isHome = window.location.pathname.split('/')[3] === 'home';
  63. var isNew = window.location.search.indexOf('is_new') !== -1;
  64. // 自动切换到最新微博
  65. if (isHome && !isNew) {
  66. window.location = '/home?is_new=1';
  67. }
  68. // 替换左上角的微博图标点击后网址为最新微博
  69. document.querySelector('a.box').setAttribute('href', '/u/' + userId + '/home?is_new=1');
  70. }
  71. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址