Skip to content

Commit 638c295

Browse files
Make comments save again after SE DOM change, fix #23
1 parent e92fc3d commit 638c295

File tree

5 files changed

+289
-337
lines changed

5 files changed

+289
-337
lines changed

Comment-History-Checker/dist/StackCommentHistoryChecker.user.js

Lines changed: 281 additions & 326 deletions
Large diffs are not rendered by default.

Comment-History-Checker/src/commentDB.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as localforageUntyped from '../node_modules/localforage/dist/localforag
88
const localforage = localforageUntyped as typeof import('localforage');
99

1010
// Only allow a get or set operation after the previous operation is complete:
11-
let lastProm: Promise<SavedComments | void> = Promise.resolve();
11+
let lastProm: Promise<SavedComments | null> = Promise.resolve(null);
1212
export const getDB = async () => {
1313
await lastProm;
1414
lastProm = localforage.getItem<SavedComments>('cpuserscriptCommentHistoryCheckerSavedComments');

Comment-History-Checker/src/userscript-metadata-block.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// @description Review the status and reception of your comments and their parent posts
44
// @author CertainPerformance
55
// @namespace https://github.com/CertainPerformance/Stack-Exchange-Userscripts
6-
// @version 1.0.10
6+
// @version 1.0.11
77
// @include /^https://(?:[^/]+\.)?(?:(?:stackoverflow|serverfault|superuser|stackexchange|askubuntu|stackapps)\.com|mathoverflow\.net)/(?:users/.*\?tab=activity|questions/\d|review/\w(?!.*/stats|.*/history))/
88
// @grant none
99
// ==/UserScript==

Comment-History-Checker/src/watchForCommentChanges/saveComment.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import { commentHrefToIds } from '../commentHrefToIds';
55
* @returns True if the database was changed, otherwise false
66
*/
77
export const saveComment = (userCommentAnchor: HTMLAnchorElement, savedComments: SavedComments) => {
8-
const dateElm = userCommentAnchor.nextElementSibling!.querySelector<HTMLElement>('.relativetime-clean')!;
8+
const commentBody = userCommentAnchor.closest('.comment-body')!;
9+
const dateElm = commentBody.querySelector<HTMLElement>('.relativetime-clean')!;
910
// The title will be something like: "2020-05-12 16:08:33Z , License: CC BY-SA 4.0"
1011
const timestamp = new Date(dateElm.title.match(/^[^,]+?(?= ?, License)/)![0]).getTime();
1112
// Some sites have a MathJax preview which is the first child of the body, rather than the comment-copy being the first child
12-
const commentHTML = userCommentAnchor.closest('.comment-body')!.querySelector('.comment-copy')!.innerHTML;
13+
const commentHTML = commentBody.querySelector('.comment-copy')!.innerHTML;
1314
const questionAnchor = document.querySelector('#question-header > h1 > a');
1415
if (!questionAnchor) {
1516
// Spam/rude question - it's likely already in the database, just don't try to update it
@@ -18,7 +19,7 @@ export const saveComment = (userCommentAnchor: HTMLAnchorElement, savedComments:
1819
const questionTitle = questionAnchor.textContent!;
1920
// Cannot just use .href of the comment-link below,
2021
// because there may be a query string which comes between the /question-title and the #commentID_postID
21-
const commentHrefAttrib = userCommentAnchor.parentElement!.querySelector('a.comment-link')!.getAttribute('href')!;
22+
const commentHrefAttrib = commentBody.querySelector('a.comment-link')!.getAttribute('href')!;
2223
const commentHref = window.location.origin + window.location.pathname + commentHrefAttrib;
2324
const { commentId } = commentHrefToIds(commentHref);
2425
const newCommentObj = {

Comment-History-Checker/src/watchForCommentTab/getApi.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ const defaultParamsArr = [
1010
['site', thisSite],
1111
];
1212

13-
type GetApi = {
14-
(method: 'comments', ids: Array<number>): Promise<ApiComments>;
15-
(method: 'questions', ids: Array<number>): Promise<ApiQuestions>;
16-
};
17-
export const getApi: GetApi = async (method: 'comments' | 'questions', ids: Array<number>) => {
13+
export const getApi = async <T extends 'comments' | 'questions'>(method: T, ids: Array<number>) => {
1814
if (!ids.length) {
1915
return { items: [] };
2016
}
@@ -24,5 +20,5 @@ export const getApi: GetApi = async (method: 'comments' | 'questions', ids: Arra
2420
const url = `https://api.stackexchange.com/2.2/${method}/${ids.join(';')}${paramsString}`;
2521
const response = await fetch(url);
2622
const responseObj = await response.json();
27-
return responseObj;
23+
return responseObj as T extends 'comments' ? ApiComments : ApiQuestions;
2824
};

0 commit comments

Comments
 (0)