Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[User script] StackExchange Chat: Mass-unstar from the star list
// ==UserScript==
// @id stackexchange-chat-mass-unstar@poke
// @name StackExchange Chat: Mass-unstar from the star list
// @namespace poke
// @version 1.2.0
// @author Patrick Westerhoff
// @include *://chat.stackoverflow.com/rooms/info/*
// @include *://chat.stackexchange.com/rooms/info/*
// @include *://chat.meta.stackexchange.com/rooms/info/*
// @homepageURL https://gist.github.com/poke/ab25d627b2c4b02db6b8
// @updateURL https://gist.githubusercontent.com/poke/ab25d627b2c4b02db6b8/raw/stackexchange-chat-mass-unstar.user.js
// @run-at document-end
// ==/UserScript==
if (!window.location.href.includes('tab=stars')) {
return;
}
const getFkey = (function() {
function fetchFkey () {
return fetch(new URL('/rooms', window.location).href, {
credentials: 'same-origin',
})
.then(result => result.text())
.then(responseText => {
const doc = new DOMParser().parseFromString(responseText, 'text/html');
return doc.getElementById('fkey').value;
});
}
let fkey = null;
return async () => {
if (!fkey) {
fkey = await fetchFkey();
}
return fkey;
};
})();
async function unstarMessage (id, fkey) {
console.log('unstar', id);
await fetch(new URL(`/messages/${id}/unstar`, window.location).href, {
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'fkey=' + fkey,
credentials: 'same-origin',
});
const message = document.getElementById('message-' + id);
message.style.background = null;
message.removeChild(message.querySelector('.flash'));
}
// intialize toggle
var selection = new Set();
function toggleSelection (evt) {
var id = this.id.substring('8');
if (!selection.delete(id)) {
selection.add(id);
}
this.style.background = selection.has(id) ? '#FF9999' : null;
}
for (const message of document.querySelectorAll('.message')) {
message.addEventListener('click', toggleSelection);
}
// intialize button
const unstarLink = document.createElement('button');
unstarLink.textContent = 'Unstar all selected messages';
unstarLink.setAttribute('style', 'float: right; font: inherit; border: 1px solid silver; border-radius: 2px;');
unstarLink.addEventListener('click', async function () {
if (!selection.size) {
return;
}
this.disabled = true;
const fkey = await getFkey();
const promises = [...selection].map(id => unstarMessage(id, fkey));
selection.clear();
await Promise.all(promises);
this.disabled = false;
});
const content = document.getElementById('content');
content.insertBefore(unstarLink, content.querySelector('.pager') || content.querySelector('.monologue'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.