Skip to content

Instantly share code, notes, and snippets.

@aequabit
Created April 21, 2020 10:57
Show Gist options
  • Save aequabit/eb0169fbc7a62208b6f69b9b3e70bae8 to your computer and use it in GitHub Desktop.
Save aequabit/eb0169fbc7a62208b6f69b9b3e70bae8 to your computer and use it in GitHub Desktop.
XenForo 2 Hide Signatures
// ==UserScript==
// @name XenForo 2 Signature Hide
// @version 1.0
// @description Removes the signature of specified users
// @author aequabit
// @match *://*/*
// @grant GM_setValue
// @grant GM_getValue
// @grant unsafeWindow
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js
// ==/UserScript==
const getStorage = () => {
const storageJson = GM_getValue('xf_signature_hide', '{}');
const storage = JSON.parse(storageJson);
// Add site storage
if (!storage.hasOwnProperty(unsafeWindow.location.host)) {
storage[unsafeWindow.location.host] = { users: [] };
}
return storage;
};
const getSiteStorage = () => getStorage()[unsafeWindow.location.host];
const saveSiteStorage = siteStorage => {
const storage = getStorage();
// Update site storage
storage[unsafeWindow.location.host] = siteStorage;
GM_setValue('xf_signature_hide', JSON.stringify(storage));
};
const setHideSignature = (userId, hide) => {
const storage = getSiteStorage();
if (hide) {
if (storage.users.includes(userId)) {
return;
}
storage.users.push(userId)
saveSiteStorage(storage);
} else {
if (!storage.users.includes(userId)) {
return;
}
storage.users.splice(storage.users.indexOf(userId), 1);
saveSiteStorage(storage);
}
setSignatureVisibility(userId, hide);
};
const isSignatureHidden = userId => {
return getSiteStorage().users.includes(userId);
};
const createIgnoreButton = userId => {
const toggleLinkText = document.createElement('span');
toggleLinkText.className = 'button-text';
toggleLinkText.innerText = (isSignatureHidden(userId) ? 'Show' : 'Hide') + ' signature';
const toggleLink = document.createElement('a');
toggleLink.className = 'button--link button';
toggleLink.href = '#';
toggleLink.onclick = e => {
setHideSignature(userId, !isSignatureHidden(userId));
toggleLinkText.innerText = (isSignatureHidden(userId) ? 'Show' : 'Hide') + ' signature';
e.preventDefault();
return false;
};
toggleLink.append(toggleLinkText);
const buttonGroup = document.createElement('div');
buttonGroup.className = 'buttonGroup';
buttonGroup.append(toggleLink);
return buttonGroup;
};
const waitForTooltip = async (tooltip) => new Promise((resolve, reject) => {
// ghetto af
const interval = setInterval(() => {
const children = $(tooltip).children();
const userLink = children.find('.memberTooltip-name > a').first();
if (userLink.length === 0) {
return;
}
clearInterval(interval);
resolve();
}, 100);
});
const insertTooltipButton = async tooltip => {
const children = $(tooltip).children();
const userLink = children.find('.memberTooltip-name > a').first();
if (userLink.length === 0) {
return;
}
const userId = userLink.data('user-id');
const tooltipActions = children.find('.memberTooltip-actions').first();
if (tooltipActions.length === 0) {
return;
}
const ignoreButton = createIgnoreButton(userId);
tooltipActions.append(ignoreButton);
};
const setSignatureVisibility = async (userId, hide) => {
for (const post of await $('.message--post').toArray()) {
const children = $(post).children();
const userLink = children.find('.message-name > a').first();
if (userLink.length === 0) {
continue;
}
const linkUserId = userLink.data('user-id');
if (linkUserId !== userId) {
continue;
}
const signature = children.find('.message-signature').first();
if (signature.length === 0) {
continue;
}
signature.css('display', hide ? 'none' : 'unset');
}
};
$(document).ready(() => {
if (unsafeWindow.XF === undefined) {
return;
}
console.log('xf_signature_hide: XenForo detected');
$('body').bind('DOMNodeInserted', async e => {
if (e.target.className === 'tooltip tooltip--member') {
await waitForTooltip(e.target);
await insertTooltipButton(e.target);
}
});
for (const userId of getSiteStorage().users) {
setSignatureVisibility(userId, true);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment