Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions src/features/postblock/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { dom } from '../../utils/dom.js';
import { getTimelineItemWrapper, filterPostElements } from '../../utils/interface.js';
import { filterPostElements, getTimelineItemWrapper } from '../../utils/interface.js';
import { registerMeatballItem, unregisterMeatballItem } from '../../utils/meatballs.js';
import { showModal, hideModal, modalCancelButton } from '../../utils/modals.js';
import { hideModal, modalCancelButton, modalCompleteButton, showModal } from '../../utils/modals.js';
import { onNewPosts, pageModifications } from '../../utils/mutations.js';
import { timelineObject } from '../../utils/react_props.js';

Expand Down Expand Up @@ -56,12 +56,58 @@ export const onStorageChanged = async function (changes) {
}
};

const migrateBlockedPosts = async ({ detail }) => {
const newBlockedPostRootIDs = JSON.parse(detail);

if (Array.isArray(newBlockedPostRootIDs)) {
window.dispatchEvent(new CustomEvent('xkit-postblock-migration-success'));

const toAdd = newBlockedPostRootIDs
.map(id => String(id))
.filter(id => !blockedPostRootIDs.includes(id));

if (toAdd.length) {
await new Promise(resolve => {
showModal({
title: 'Add blocked posts?',
message: [
`Would you like to import ${toAdd.length} blocked post id${
toAdd.length === 1 ? '' : 's'
} from New XKit to XKit Rewritten?`,
],
buttons: [
modalCancelButton,
dom('button', { class: 'blue' }, { click: resolve }, ['Confirm']),
],
});
});

blockedPostRootIDs.push(...toAdd);
await browser.storage.local.set({ [storageKey]: blockedPostRootIDs });

showModal({
title: 'Success',
message: `Imported ${toAdd.length > 1 ? `${toAdd.length} blocked posts` : 'a blocked post'}!`,
buttons: [modalCompleteButton],
});
} else {
showModal({
title: 'No new blocked posts!',
message: 'Your XKit Rewritten configuration has these posts blocked already.',
buttons: [modalCompleteButton],
});
}
}
};

export const main = async function () {
({ [storageKey]: blockedPostRootIDs = [] } = await browser.storage.local.get(storageKey));

registerMeatballItem({ id: meatballButtonId, label: meatballButtonLabel, onclick: onButtonClicked });

onNewPosts.addListener(processPosts);

window.addEventListener('xkit-postblock-migration', migrateBlockedPosts);
};

export const clean = async function () {
Expand Down