Skip to content

Instantly share code, notes, and snippets.

@sp00n
Last active March 30, 2024 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sp00n/61c2e6cf33553bf4d56062783a9228d3 to your computer and use it in GitHub Desktop.
Save sp00n/61c2e6cf33553bf4d56062783a9228d3 to your computer and use it in GitHub Desktop.
mtb-news.de Userscript, um alle neuen Beiträge mit einem Klick zu öffnen
// ==UserScript==
// @name mtb-news.de - Add "Open New Notifications" Button
// @namespace net.sp00n
// @match https://www.mtb-news.de/forum/*
// @grant none
// @version 1.4
// @author sp00n82
// @description This needs an additional setting, at least for Firefox: Firefox Options -> Privacy & Security -> Permissions -> Block pop-up windows -> Exceptions -> Add "https://www.mtb-news.de" to the Allowed Websites list
// @downloadURL https://gist.github.com/sp00n/61c2e6cf33553bf4d56062783a9228d3
// @license Creative Commons CC BY-SA 3.0 DE
// ==/UserScript==
// Adds an "open new" button to the header of the notification popup
(function() {
console.log("Init mtb-news notification observer");
const buttonString = "Neue öffnen";
const domain = "https://www.mtb-news.de";
// Add the open all link element
const addOpenAllLinkFunc = function(ele) {
const header = ele.querySelector(".menu-header");
// The open new link already exists
if ( header.querySelector(".open-all-notifications") ) {
console.log("Link already exits, aborting!");
return false;
}
// Create the link element
let button = document.createElement("button");
button.classList.add("open-all-notifications", "button--primary", "button", "button--icon", "button--icon--notificationsOn");
button.style.cssText = `
font-size: 0.7em;
position: relative;
left: 1.5em;
`;
let span = document.createElement("span");
span.classList.add("button-text");
span.textContent = buttonString;
button.appendChild(span);
// And the click observer
button.addEventListener("click", function(event) {
event.preventDefault();
// Handle only left and middle mouse clicks
if ( event.which > 2 ) {
console.log("Wrong mouse button, aborting");
return;
}
const list = ele.querySelector(".listPlain");
const lis = list.querySelectorAll("li.menu-row.is-unread");
const aEles = [...lis].map(function(entry) { // NodeList to Array
return entry.querySelector("a.fauxBlockLink-blockLink");
});
const links = aEles.map(function(entry) {
return entry.getAttribute("href");
});
let counter = 1;
// Open each link in a new window (tab)
links.forEach(function(link, key) {
// Ignore links to other domains (e.g. Winterpokal)
if ( link.indexOf("http") == 0 || link.indexOf("ftp") == 0 ) {
return;
}
window.open(domain + link, `_newtab_${counter}_${Date.now()}`);
counter++;
});
});
header.appendChild(button);
};
// Observe the notification popup element
const observeElement = document.querySelector("div.menu[data-href='/forum/account/alerts-popup']");
// Set up a Mutation Observer
const config = {
attributes : true
, attributeOldValue : true
, attributeFilter : ["class"]
};
// Observe it for class changes (which happens when it gets openend)
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if ( mutation.attributeName == "class" ) {
let ele = mutation.target;
// The popup has finished opening, now we can add the open all link
if ( ele.classList.contains("is-active") && ele.classList.contains("is-complete") && !ele.classList.contains("is-transitioning") ) {
addOpenAllLinkFunc(ele);
}
}
});
});
observer.observe(observeElement, config);
})();
@Masterzero123
Copy link

I liked your code, I implemented it in Tampermokey,
I sent you an email so you could help me with something related to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment