scripts/userscripts/messenger-dynamic-sidebar.user.js

84 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-11-26 06:43:15 +00:00
// ==UserScript==
// @name Dynamic Sidebar
// @namespace tretrauit-dev
// @match *://www.messenger.com
// @icon https://genshin.hoyoverse.com/favicon.ico
// @grant none
// @version 1.0
// @author tretrauit
// @description Dynamic Sidebar for Facebook Messenger (messenger.com)
// @homepageURL https://gitlab.com/tretrauit/scripts
// @supportURL https://gitlab.com/tretrauit/scripts/-/issues
// @downloadURL https://gitlab.com/tretrauit/scripts/-/raw/main/userscripts/messenger-dynamic-sidebar.user.js
// ==/UserScript==
function injectCSS(css) {
const style = document.createElement('style');
style.appendChild(document.createTextNode(css));
document.head.appendChild(style);
}
function findElement(tag, properties) {
const elements = document.querySelectorAll(tag);
elementLoop:
for (const element of elements) {
for (const [key, value] of Object.entries(properties)) {
if (element.getAttribute(key) != value) {
continue elementLoop;
}
}
return element;
}
}
function getAncestor(element, level) {
2023-06-11 09:18:20 +00:00
if (element == null) {
return null;
}
2022-11-26 06:43:15 +00:00
for (let i = 0; i < level; i++) {
element = element.parentNode;
}
return element;
}
console.log("Scanning class for components...");
2023-06-11 09:18:20 +00:00
// Search box
2022-11-26 06:43:15 +00:00
let searchBox = findElement("input", {"aria-autocomplete": "list"});
2023-06-11 09:18:20 +00:00
if (searchBox == null) {
console.warn("Failed to get searchBox element.");
throw new Error();
2022-11-26 06:43:15 +00:00
}
2023-06-11 09:18:20 +00:00
searchBox = getAncestor(searchBox, 7);
// Header & Text header
let textHeader = findElement("span", {"style": "line-height: var(--base-line-clamp-line-height); --base-line-clamp-line-height:28px;"});
2022-11-26 06:43:15 +00:00
let header;
2023-06-11 09:18:20 +00:00
if (textHeader == null) {
console.warn("Failed to get textHeader element.");
throw new Error();
2022-11-26 06:43:15 +00:00
}
2023-06-11 09:18:20 +00:00
header = getAncestor(textHeader, 7);
textHeader = textHeader.childNodes[0];
// Unread indicator
2022-11-26 06:43:15 +00:00
let unreadIndicator = findElement("span", {"data-visualcompletion": "ignore"});
2023-06-11 09:18:20 +00:00
// Action bar
2022-11-26 06:43:15 +00:00
let actionBar = findElement("div", {"aria-expanded": "false"});
2023-06-11 09:18:20 +00:00
if (actionBar == null) {
console.warn("Failed to get actionBar element.");
throw new Error();
2022-11-26 06:43:15 +00:00
}
2023-06-11 09:18:20 +00:00
actionBar = actionBar.parentNode;
// Chats
2022-11-26 06:43:15 +00:00
let chats = findElement("div", {"aria-label": "Chats"});
2023-06-11 09:18:20 +00:00
if (chats == null) {
console.warn("Failed to get chats element.");
throw new Error();
2022-11-26 06:43:15 +00:00
}
2023-06-11 09:18:20 +00:00
chats = chats.parentNode;
// Print elements
2022-11-26 06:43:15 +00:00
console.log("Search box:", searchBox);
console.log("Header:", header);
console.log("Text header:", textHeader);
console.log("Unread message indicator:", unreadIndicator);
console.log("Action bar:", actionBar);
2023-06-11 09:18:20 +00:00
console.log("Chat tab:", chats);