Skip to content
This repository was archived by the owner on Aug 27, 2025. It is now read-only.

Commit 533fe6b

Browse files
committed
feat: endless observer for tweet status btn
1 parent 9e25811 commit 533fe6b

3 files changed

Lines changed: 17 additions & 9 deletions

File tree

src/common/utils.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { debounce } from 'lodash-es';
22

33
const interval = 200;
44
const maxCount = 10000 / interval;
5-
export function observe(selector: string, callback: (ele: Element) => void) {
5+
export function observe(selector: string, callback: (ele: Element) => void, endless?: boolean) {
66
let observer: MutationObserver;
77
let cache: Element;
88
const cb = (result: Element) => {
@@ -14,7 +14,7 @@ export function observe(selector: string, callback: (ele: Element) => void) {
1414
const run = () => {
1515
let currentCount = 0;
1616
const result = document.querySelector(selector);
17-
if (result) {
17+
if (result && !endless) {
1818
cb(result);
1919
} else {
2020
if (observer) {
@@ -23,13 +23,19 @@ export function observe(selector: string, callback: (ele: Element) => void) {
2323
observer = new MutationObserver(
2424
debounce(() => {
2525
const result = document.querySelector(selector);
26-
if (result) {
27-
observer.disconnect();
28-
cb(result);
29-
} else if (currentCount > maxCount) {
30-
observer.disconnect();
26+
if (!endless) {
27+
if (result) {
28+
observer.disconnect();
29+
cb(result);
30+
} else if (currentCount > maxCount) {
31+
observer.disconnect();
32+
} else {
33+
currentCount++;
34+
}
3135
} else {
32-
currentCount++;
36+
if (result) {
37+
cb(result);
38+
}
3339
}
3440
}, interval),
3541
);

src/content-script/hooks/twitter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class TwitterHook {
3333
callback: async (el) => {
3434
this.mountSyncOldTweets();
3535
},
36+
endless: true,
3637
},
3738
];
3839

src/content-script/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import '../css/lib.css';
1111
export interface Hook {
1212
selector: string;
1313
callback: (el: Element) => void;
14+
endless?: boolean;
1415
}
1516

1617
class CrossSyncContentScript {
@@ -29,7 +30,7 @@ class CrossSyncContentScript {
2930
switch (new URL(window.location.href).host) {
3031
case 'twitter.com':
3132
new TwitterHook(this).hooks.forEach((h) => {
32-
observe(h.selector, h.callback);
33+
observe(h.selector, h.callback, h.endless);
3334
});
3435
break;
3536
default:

0 commit comments

Comments
 (0)