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

88 lines
2.5 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
2024-03-06 16:48:06 +00:00
// @downloadURL https://git.tretrauit.me/tretrauit/scripts/raw/branch/main/userscripts/messenger-dynamic-sidebar.user.js
2022-11-26 06:43:15 +00:00
// ==/UserScript==
function injectCSS(css) {
const style = document.createElement("style");
style.appendChild(document.createTextNode(css));
document.head.appendChild(style);
2022-11-26 06:43:15 +00:00
}
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;
}
2022-11-26 06:43:15 +00:00
}
function getAncestor(element, level) {
if (element == null) {
return null;
}
for (let i = 0; i < level; i++) {
element = element.parentNode;
}
return element;
2022-11-26 06:43:15 +00:00
}
console.log("Scanning class for components...");
2023-06-11 09:18:20 +00:00
// Search box
2023-08-21 18:28:47 +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
2023-08-21 18:28:47 +00:00
let textHeader = findElement("span", {
style:
"line-height: var(--base-line-clamp-line-height); --base-line-clamp-line-height:28px;",
2023-08-21 18:28:47 +00:00
});
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
2023-08-21 18:28:47 +00:00
let unreadIndicator = findElement("span", {
"data-visualcompletion": "ignore",
2023-08-21 18:28:47 +00:00
});
2023-06-11 09:18:20 +00:00
// Action bar
2023-08-21 18:28:47 +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
2023-08-21 18:28:47 +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);