Discussions » Creation Requests

mega.nz - Set tab titles based on content

§
Posted: 2019-10-19
Edited: 2019-10-19

mega.nz - Set tab titles based on content

I've been trying to figure out how to do this for several hours but my Javascript knowledge is extremely limited and I can't get anything to work properly.

My problem is that with multiple tabs open containing shared Mega files and folders they all look like this:

This is quite unhelpful and I’d really like something more descriptive based on the tabs’ contents.

I've found there seem to be two main scenarios: single files and folders.

With single files the script would need to get the text from <span class="filename"> and set it as the page title.

When there are folders there is a breadcrumb showing the file path (<div class="fm-breadcrumbs-block"). Ideally it would be nice to show the full path in the title – i.e. in the example I posted the page title would show as example folder/1/2/3 – and for it to update as different folders are selected, but otherwise being able to show just the outermost folder would obviously be simpler and still useful.

 

Based on bits taken from other scripts and looking stuff up I tried making this (for the single file scenario), but it only sets the page title to undefined :/. I can't tell if that's because I'm just overlooking something simple, or if the whole needs doing differently.

// ==UserScript==
// @name         Mega tab names
// @match        *://*mega.nz/*
// @grant        none
// ==/UserScript==
(function() {
'use strict';
waitForLoad();
function run() {
    var filename = document.getElementsByClassName("filename").innerText;
    document.title = filename;
}
function waitForLoad() {
    var spanLoaded = document.getElementsByClassName("filename").length;
    if(spanLoaded > 0) {
        run();
    } else {
        setTimeout(waitForLoad, 50);
    }
}
})();
wOxxOmMod
§
Posted: 2019-10-19

getElementsByClassName returns an array-like collection so you need to add [0] to get the first element: document.getElementsByClassName("filename")[0].innerText however it's easier to use querySelector instead and specify the class name in CSS notation with a dot: document.querySelector(".filename").innerText

§
Posted: 2019-10-19

Superb! Thank you for your help. <3

Post reply

Sign in to post a reply.