Add button and hotkey to hide contact names and avatars on web.whatsapp.com
当前为
// ==UserScript==
// @name Whatsapp Web Privacy Mode
// @namespace graphen
// @version 1.7
// @description Add button and hotkey to hide contact names and avatars on web.whatsapp.com
// @author Graphen
// @match https://web.whatsapp.com/
// @grant none
// ==/UserScript==
/* jshint esversion: 6 */
(function() {
'use strict';
var hidden = false;
var defaultbkg = 'lightgrey';
var prevName = '';
window.addEventListener('load', () => {
checkScreenLoaded();
document.addEventListener("keydown", function(e) {
var keyCode = e.keyCode;
if(keyCode==118) {
toggleShowHide();
}
}, false);
});
function checkScreenLoaded() {
setTimeout(function(){
if (document.getElementById('side')) {
addButton('Hide / F7', toggleShowHide);
console.log("WA Privacy: Button added.");
} else {
checkScreenLoaded();
console.log("WA Privacy: Not loaded.");
}
}, 1000);
}
function addButton(text, onclick, cssObj) {
cssObj = cssObj || {position: 'absolute', top: '15px', left:'70px', 'z-index': 5000, 'font-weight':'bold', border:'black solid', 'border-radius':'10px',
padding:'4px', 'background-color': defaultbkg, 'min-width': '75px', 'box-shadow':'grey 3px 3px 0px 0px'};
let button = document.createElement('button'), btnStyle = button.style;
try{
document.getElementById('side').appendChild(button);
}
catch(err){
console.log("addButton: " + err);
}
button.innerHTML = text;
button.onclick = onclick;
button.classList = ['show-hide-btn unpressed'];
button.onmouseover = function() {
button.style["background-color"] = 'salmon';
};
button.onmouseout = function() {
button.style["background-color"] = defaultbkg;
};
Object.keys(cssObj).forEach(key => btnStyle[key] = cssObj[key]);
return button;
}
function toggleShowHide() {
var btn = document.getElementsByClassName('show-hide-btn')[0];
var panel = document.getElementById('pane-side');
var groupicon = document.querySelector('#main .O90ur');
if (hidden) {
console.log("WA Privacy: Toggled show.");
panel.setAttribute('style', 'display:block');
defaultbkg = 'lightgrey';
btn.innerHTML = 'Hide / F7';
btn.style.left = '70px';
btn.style.top = '15px';
btn.style["box-shadow"] = 'grey 3px 3px 0px 0px';
try { // show
document.querySelector('#main ._1wjpf').innerHTML = prevName;
document.querySelector('#main ._1wjpf').title = prevName;
document.querySelector('#main .Qgzj8.gqwaM').setAttribute('style', 'opacity:1 !important;');
// subheadline for group chats
if (groupicon !== null) {
groupicon.setAttribute('style', 'display:block;');
}
} catch(err) {
console.log("toggleShow: " + err);
}
prevName = '';
} else {
console.log("WA Privacy: Toggled hide.");
panel.setAttribute('style', 'display:none');
defaultbkg = 'grey';
btn.innerHTML = 'Show / F7';
btn.style.left = '73px';
btn.style.top = '18px';
btn.style["box-shadow"] = '';
try { // hide
prevName = document.querySelector('#main ._1wjpf').innerHTML;
document.querySelector('#main ._1wjpf').innerHTML = randomName();
document.querySelector('#main ._1wjpf').removeAttribute('title');
document.querySelector('#main .Qgzj8.gqwaM').setAttribute('style', 'opacity:0 !important;');
// subheadline for group chats
if (groupicon !== null) {
groupicon.setAttribute('style', 'display:none;');
}
} catch(err) {
console.log("toggleHide: " + err);
}
}
document.getElementsByClassName('show-hide-btn')[0].style["background-color"] = defaultbkg;
hidden = !hidden;
}
function randomName() {
var names = ['Grace Kelly', 'Nicolas Cage', 'Albert Einstein', 'Angus Young', 'Katy Perry', 'Leonardo DiCaprio', 'Cleopatra', 'Anonymous Pasta Lovers',
'Karl Lagerfeld', 'Holy Father', 'Homer Simpson', 'Barney Stinson', 'Walter White', 'Queen Victoria', 'Sheldon Cooper', 'Mahatma Gandhi'];
return names[Math.floor(Math.random() * names.length)];
}
})();