Skip to content

Instantly share code, notes, and snippets.

@f2404
Last active July 29, 2020 15:47
Show Gist options
  • Select an option

  • Save f2404/df7c019ad1d44d408e6bb4890b3c1e5e to your computer and use it in GitHub Desktop.

Select an option

Save f2404/df7c019ad1d44d408e6bb4890b3c1e5e to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Revivers Blacklist
// @namespace lugburz.blacklist
// @version 0.4.2
// @description Custom blacklist.
// @author Lugburz
// @match https://www.torn.com/*
// @require https://github.com/f2404/torn-userscripts/raw/31f4faa6da771b7a16cf732c1a78970506effeeb/lib/lugburz_lib.js
// @connect script.google.com
// @connect script.googleusercontent.com
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_deleteValue
// @grant GM_addStyle
// @grant GM_notification
// @grant GM_xmlhttpRequest
// ==/UserScript==
const MYKEY = '';
const L_URL = 'https://script.google.com/macros/s/AKfycbxJcj9BwELaGqRq7MRUMAQKtbzI20g8Dkpr0DlaySODo98TsNPP/exec';
const P_URL = 'https://script.google.com/macros/s/AKfycbwZq6OG7zFXlce1l9IN8DSf5xQMXyZtUySmnIYpSgcMNJZX-EBC/exec';
GM_addStyle(`
.blacklist-div {
padding: 0px 10px;
}
.blacklisted {
background: LightCoral !important;
}
.blacklist-button {
color: #069;
text-decoration: none;
cursor: pointer;
-webkit-transition: color .2s ease;
-o-transition: color .2s ease;
transition: color .2s ease;
}
.centered-button {
vertical-align: top;
line-height: 40px;
}
.scrollabletextbox {
height: 34px;
width: 230px;
font: inherit;
overflow-y: scroll;
resize: none;
}
.blacklist-cb {
margin-left: 5px;
margin-right: 5px;
}
`);
var MYUID;
const google_api = async (args) => {
const a = args.split('.');
if (a.length !== 4) throw(`Bad argument in google_api(args, key): ${args}`);
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: "GET",
url: L_URL + '?id=all&my_id=' + a[0] + '&my_name=' +a[1] + '&my_faction_id=' + a[2] + '&my_faction_name=' + a[3] + '&my_key=' + MYKEY,
headers: {
"Content-Type": "application/json"
},
onload: (response) => {
try {
resolve(response.responseText);
} catch (err) {
reject(err);
}
},
onerror: (err) => {
reject(err);
}
});
});
}
function checkPermissions(response) {
if (response == 'Permission denied') {
alert("You're not an authorized user, uninstall the script to remove yourself from Revivers Blacklist");
}
try {
JSON.parse(response);
return true;
} catch (e) {
console.log(response);
GM_deleteValue('blacklist');
return false;
}
}
async function getBlacklist() {
const myName = GM_getValue('myName') || 'no data';
const myFactionId = GM_getValue('myFactionId') || 'no data';
const myFactionName = GM_getValue('myFactionName') || 'no data';
const blacklist = await google_api(`${MYUID}.${myName}.${myFactionId}.${myFactionName}`);
console.log(blacklist);
if (checkPermissions(blacklist)) {
GM_setValue('blacklist', blacklist);
GM_setValue('blacklist_timestamp', Date.now());
maybeUpdateProfile();
maybeUpdateFactionPage();
}
}
function sendPost(url, data, callback) {
GM_xmlhttpRequest({
method: "POST",
url: url,
data: data,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(response) {
callback(response);
}
});
}
function removeId(id, targetFactionId = '') {
const myName = GM_getValue('myName') || 'no data';
const myFactionId = GM_getValue('myFactionId') || 'no data';
const myFactionName = GM_getValue('myFactionName') || 'no data';
const data = $.param({action: "remove", id: id, my_id: MYUID, my_name: myName, my_faction_id: myFactionId, my_faction_name: myFactionName,
target_faction_id: targetFactionId, my_key: MYKEY});
sendPost(L_URL, data, function(response) {
if (checkPermissions(response.responseText)) {
const json = JSON.parse(response.responseText);
GM_setValue('blacklist', response.responseText);
GM_notification((id && id != 'N/A' ? 'User '+id : 'Faction '+targetFactionId) + ' has been removed.', 'Revivers Blacklist');
maybeUpdateProfile();
maybeUpdateFactionPage();
} else {
GM_notification(response.responseText, 'Revivers Blacklist');
}
});
}
function blacklistId(id, reason, targetName, targetFactionId, targetFactionName) {
const myName = GM_getValue('myName') || 'no data';
const myFactionId = GM_getValue('myFactionId') || 'no data';
const myFactionName = GM_getValue('myFactionName') || 'no data';
const data = $.param({action: "add", id: id, my_id: MYUID, my_name: myName, my_faction_id: myFactionId, my_faction_name: myFactionName,
target_name: targetName, target_faction_id: targetFactionId, target_faction_name: targetFactionName, reason: reason, my_key: MYKEY});
sendPost(L_URL, data, function(response) {
if (checkPermissions(response.responseText)) {
const json = JSON.parse(response.responseText);
GM_setValue('blacklist', response.responseText);
GM_notification((id && id != 'N/A' ? 'User '+id : 'Faction '+targetFactionId) + ' has been blacklisted.', 'Revivers Blacklist');
maybeUpdateProfile();
maybeUpdateFactionPage();
} else {
GM_notification(response.responseText, 'Revivers Blacklist');
}
});
}
function maybeUpdateProfile() {
if ($(location).attr('href').includes('profiles.php')) {
$('#multireason').val('');
$('#blacklistRemove').remove();
$('#blacklistPermissionCb').remove();
$('label[for="blacklistPermissionCb"]').remove();
const id = RegExp(/XID=(\d+)/).exec($(location).attr('href'))[1];
updateProfile(id);
}
}
function getUid() {
const match = /uid=(\d+)/.exec(document.cookie);
if (match) {
return match[1];
}
return '';
}
function getReasons(id) {
try {
return JSON.parse(GM_getValue('blacklist'))[id];
} catch (e) {
console.log(GM_getValue('blacklist'));
return null;
}
}
function getFactionReasons(targetFactionId) {
try {
if (!JSON.parse(GM_getValue('blacklist')).factions) {
return null;
}
return JSON.parse(GM_getValue('blacklist')).factions[targetFactionId];
} catch (e) {
console.log(GM_getValue('blacklist'));
return null;
}
}
function enableProfileButtons(div, enable) {
const buttonsDiv = $(div).parent().find('div.buttons-list');
$(buttonsDiv).children().each(function() {
if (enable) {
//if (!$(this).attr('wasDisabled') && !$(this).hasClass('cross')) {
//$(this).removeClass('disabled');
//$(this).addClass('active');
//}
$(this).removeClass('blacklisted');
//$(this).off('click');
//$(this).attr('wasDisabled', '');
} else {
//if ($(this).hasClass('disabled')) {
//$(this).attr('wasDisabled', 1);
//}
//$(this).removeClass('active');
//$(this).addClass('disabled');
$(this).addClass('blacklisted');
//$(this).on('click', function() {
//return false;
//});
}
});
}
function updateProfile(id) {
const list = $('#profileroot').find('ul.info-table').children();
const targetName = $(list[0]).find('div.user-info-value > span').text().trim().replace(/\s+\[.+/, '');
const targetFactionName = $(list[2]).find('div.user-info-value').find('a').text().trim();
const href = $(list[2]).find('div.user-info-value').find('a').attr('href');
const targetFactionId = href ? href.trim().replace(/.+ID=/, '') : 0;
const reasons = getReasons(id);
const factionReasons = getFactionReasons(targetFactionId);
// Permissions
const data = $.param({action: "get", target_id: id, target_faction_id: targetFactionId, my_id: MYUID, my_key: MYKEY});
sendPost(P_URL, data, function(response) {
if ($('#blacklistPermissionCb').size() < 1 && (response.responseText == 'True' || response.responseText == 'False')) {
const cb = '<input type="checkbox" class="blacklist-cb" id="blacklistPermissionCb" name="blacklistPermissionCb"><label for="blacklistPermissionCb">Revivers Blacklist user</label>';
$('div.profile-buttons.profile-action').find('div.title-black').append(cb);
$('#blacklistPermissionCb').prop('checked', response.responseText == 'True');
$('#blacklistPermissionCb').change(function() {
if ($(this).prop('checked')) {
const list = $('#profileroot').find('ul.info-table').children();
const targetName = $(list[0]).find('div.user-info-value > span').text().trim().replace(/\s+\[.+/, '');
const targetFactionName = $(list[2]).find('div.user-info-value').find('a').text().trim();
const href = $(list[2]).find('div.user-info-value').find('a').attr('href');
const targetFactionId = href ? href.trim().replace(/.+ID=/, '') : 0;
const data2 = $.param({action: 'add', target_id: id, target_name: targetName, target_faction_name: targetFactionName, target_faction_id: targetFactionId,
my_id: MYUID, my_key: MYKEY});
sendPost(P_URL, data2, function(response) {
if (response.responseText == 'Success') {
GM_notification(id + ' has been given permission to use the script.', 'Revivers Blacklist');
maybeUpdateProfile();
} else {
console.log(response.responseText);
}
});
} else {
const data2 = $.param({action: 'remove', target_id: id, my_id: MYUID, my_key: MYKEY});
sendPost(P_URL, data2, function(response) {
if (response.responseText == 'Success') {
GM_notification(id + '\'s permission to use the script has been revoked.', 'Revivers Blacklist');
maybeUpdateProfile();
} else {
console.log(response.responseText);
}
});
}
});
} else if ($('#blacklistPermissionCb').size() > 0 && (response.responseText == 'True' || response.responseText == 'False')) {
$('#blacklistPermissionCb').prop('checked', response.responseText == 'True');
}
});
// Blacklist
if ($('#blacklist').size() < 1) {
const div = '<div class="blacklist-div"><a id="blacklist" class="blacklist-button centered-button" href="#">Blacklist </a><textarea id="multireason" class="scrollabletextbox" cols="35" disabled></textarea>' +
'<p id="preason" style="display: none;"><label for="reason">Reason: </label>' +
'<input name="reason" type="text" size="35" style="height: 34px; margin-top: 3px;"> <a class="blacklist-button" id="blacklistAdd" href="#">Okay</a></p></div>';
$('div.empty-block').append(div);
$('#blacklist').on('click', function() {
// put "p" on top of '#multireason'
const position = $('#multireason').position();
$('#preason').css({position: 'absolute', zIndex: 5000, top: position.top, left: position.left});
$('#multireason').toggle();
$('#preason').toggle();
$('#blacklistRemove').toggle();
});
$('#blacklistAdd').on('click', function() {
const reason = $('#preason').find('input').val();
if (reason) {
$('#preason').hide();
$('#multireason').show();
$('#blacklistRemove').show();
blacklistId(id, reason, targetName, targetFactionId, targetFactionName);
} else {
alert('Must add a reason!');
}
});
}
if ((reasons || factionReasons) && !$('#multireason').val()) {
$('div.empty-block').closest('div.profile-container').addClass('blacklisted');
enableProfileButtons($('div.empty-block'), false);
let myReasons = [];
for (let r in reasons) {
if (reasons[r][0] == MYUID) {
myReasons.push(r);
}
$('#multireason').val($('#multireason').val() + reasons[r][1] + '\n');
}
for (let r in factionReasons) {
if (factionReasons[r][0] == MYUID) {
myReasons.push(r);
}
$('#multireason').val($('#multireason').val() + factionReasons[r][1] + '\n');
}
if (myReasons.length) {
const a = '<a id="blacklistRemove" class="blacklist-button centered-button" href="#"> Remove</a>';
$(a).insertAfter($('#multireason'));
$('#blacklistRemove').on('click', function() {
removeId(id);
});
}
} else if (!reasons && !factionReasons) {
$('div.empty-block').closest('div.profile-container').removeClass('blacklisted');
enableProfileButtons($('div.empty-block'), true);
}
}
function updateHospitalview() {
$('ul.user-info-list-wrap > li').each(function(index) {
let id = $(this).find('a.user.name').attr('href');
if (id) {
id = id.replace(/\/?profiles.php\?XID=/, '');
const reasons = getReasons(id);
if (reasons) {
$(this).css('background-color', 'LightCoral');
}
}
});
}
function maybeUpdateFactionPage() {
if ($(location).attr('href').includes('factions.php')) {
$('#multireason').val('');
$('#blacklistRemove').remove();
$('#blacklistPermissionCb').remove();
$('label[for="blacklistPermissionCb"]').remove();
updateFactionPage();
}
}
function updateFactionPage() {
const match = RegExp(/profile\&ID=(\d+)/).exec($(location).attr('href'));
if (!match) {
return;
}
const targetFactionId = match[1];
const targetFactionName = $('#factions').find('div.faction-info.bottom-round').attr('data-name').trim();
const factionReasons = getFactionReasons(targetFactionId);
// Blacklist
if ($('#blacklist').size() < 1) {
const div = '<div class="blacklist-div"><a id="blacklist" class="blacklist-button centered-button" href="#">Blacklist </a><textarea id="multireason" class="scrollabletextbox" cols="35" disabled></textarea>' +
'<p id="preason" style="display: none;"><label for="reason">Reason: </label>' +
'<input name="reason" type="text" size="35" style="height: 34px; margin-top: 3px;"> <a class="blacklist-button" id="blacklistAdd" href="#">Okay</a></p></div>';
$(div).insertBefore($('#war-react-root'));
const delim = '<hr class="page-head-delimiter m-top10">';
$(delim).insertBefore($('div.blacklist-div'));
$('#multireason').width($('div.blacklist-div').width() - 110);
$('#blacklist').on('click', function() {
// put "p" on top of '#multireason'
const position = $('#multireason').position();
$('#preason').css({position: 'absolute', zIndex: 5000, top: position.top, left: position.left});
$('#multireason').toggle();
$('#preason').toggle();
$('#blacklistRemove').toggle();
});
$('#blacklistAdd').on('click', function() {
const reason = $('#preason').find('input').val();
if (reason) {
$('#preason').hide();
$('#multireason').show();
$('#blacklistRemove').show();
blacklistId('N/A', reason, 'N/A', targetFactionId, targetFactionName);
} else {
alert('Must add a reason!');
}
});
}
if (factionReasons && !$('#multireason').val()) {
$('#factions').addClass('blacklisted');
let myReasons = [];
for (let r in factionReasons) {
if (factionReasons[r][0] == MYUID) {
myReasons.push(r);
}
$('#multireason').val($('#multireason').val() + factionReasons[r][1] + '\n');
}
if (myReasons.length) {
const a = '<a id="blacklistRemove" class="blacklist-button centered-button" href="#"> Remove</a>';
$(a).insertAfter($('#multireason'));
$('#blacklistRemove').on('click', function() {
removeId('N/A', targetFactionId);
});
}
} else if (!factionReasons) {
$('#factions').removeClass('blacklisted');
}
}
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
const newNodes = mutation.addedNodes;
if (newNodes) {
$(newNodes).each(function() {
if ($(this).attr('id') && $(this).attr('id') == 'profile-mini-root') {
console.log('profile-mini-root found');
observer.disconnect();
observer.observe($('#profile-mini-root').get(0), { subtree: true, childList: true });
} else if ($(this).attr('class') && $(this).attr('class').includes('-profile-mini-_userProfileWrapper___')) {
let id = $(this).find('div[class^=-profile-mini-_userWrap__]').find('a').attr('href');
if (id) {
id = id.replace(/\/?profiles.php\?XID=/, '');
let targetFactionId = 0;
if ($(this).find('div[class^=-profile-mini-_factionWrap__]').size() > 0) {
targetFactionId = $(this).find('div[class^=-profile-mini-_factionWrap__]').find('a').attr('href');
targetFactionId = targetFactionId.replace(/\/factions.php\?step=profile\&ID=/, '');
}
console.log('mini profile id=' + id + ', faction id=' + targetFactionId);
const reasons = getReasons(id);
const factionReasons = getFactionReasons(targetFactionId);
if (reasons || factionReasons) {
$(this).find('div.buttons-wrap').addClass('blacklisted');
enableProfileButtons($(this).find('div.buttons-wrap'), false);
$(this).find('div.buttons-list').children().each(function() {
$(this).addClass('blacklisted');
});
}
}
}
});
}
});
});
var PERIOD = 15*60*1000; // 15 min
function maybeUpdateBlacklist() {
if (!GM_getValue('blacklist') || !GM_getValue('blacklist_timestamp') || Date.now() - GM_getValue('blacklist_timestamp') > PERIOD) {
getBlacklist();
}
}
(function() {
'use strict';
// Your code here...
MYUID = getUid();
if ($("#sidebarroot").find('a[class^=menu-value___]').size() > 0) {
const myName = $("#sidebarroot").find('a[class^=menu-value___]').text().trim();
GM_setValue('myName', myName);
}
maybeUpdateBlacklist(); // first run or outdated local copy
setInterval(getBlacklist, PERIOD); // periodic
if ($(location).attr('href').includes('profiles.php')) {
const id = RegExp(/XID=(\d+)/).exec($(location).attr('href'))[1];
updateProfile(id);
} else if ($(location).attr('href').includes('hospitalview.php')) {
updateHospitalview();
} else if ($(location).attr('href').includes('factions.php')) {
updateFactionPage();
} else if ($(location).attr('href').includes('index.php')) {
const links = $('div.content').find('ul.info-cont-wrap').find('a');
$(links).each(function() {
if ($(this).attr('href').startsWith('factions.php?step=profile&ID=')) {
const myFactionId = $(this).attr('href').replace('factions.php?step=profile&ID=', '').trim();
const myFactionName = $(this).text().trim();
GM_setValue('myFactionId', myFactionId);
GM_setValue('myFactionName', myFactionName);
}
});
}
const wrapper = $('#body');
observer.observe($(wrapper).get(0), { subtree: true, childList: true });
ajax((page) => {
if (page == "profiles") {
const id = RegExp(/XID=(\d+)/).exec($(location).attr('href'))[1];
updateProfile(id);
} else if (page == "hospitalview") {
updateHospitalview();
} else if (page == "factions") {
updateFactionPage();
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment