Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Ben Os
Ben Os
20,008 Points

Userscript works only on pages served from backend, but not on frontend in a SPA way

I have the following userscript I run on Greasemonkey/Tampermonkey.

I run it on facebook.com which serves some of the webpages from backend, in bootstrapping, and some others on the fly, in front-end, via HRO, just as a Single Page Application (SPA) would.

    // ==UserScript==
    // @name        facebook
    // @namespace   nms
    // @include     http://*.facebook.com/*
    // @include     https://*.facebook.com/*
    // @version     1
    // @grant       none
    // ==/UserScript==

    setTimeout( () => {
        // generalStuff:
            document.querySelectorAll(' #left_nav_section_nodes, .fbChatSidebar ').forEach( (e)=> {
                e.style.visibility = "hidden";
            });

    }, 1000);

If I run this script on console, even in HRO based webpages, it runs fine, but when runned from Greasemoneky/Tampermonkey it won't run in these particular webpages.

How could I make the script to work without problem on SPA-like webpages as well?

Steven Parker Andrew Chalkley

Joel Kraft
Joel Kraft
Treehouse Guest Teacher

This question involves specific software that we don't teach or work with at treehouse, so you might get more luck by posting a question like this on stackoverflow.com. Just doing a quick search for greasemonkey revealed almost 8000 results, so your question has potentially either been answered there already, or could be answered by one of the users there if you were to post. Hope that helps. Good luck!

Ben Os
Ben Os
20,008 Points

Hi Joel. I already asked this question a few days ago there, but no one answered (link below)... I also tried setInterval and it didn't help. When I'll know it enough, I'll try event delegation.

https://stackoverflow.com/questions/45427068/userscript-works-only-on-pages-served-from-backend-but-not-on-frontend-in-a-spa

Joel Kraft
Joel Kraft
Treehouse Guest Teacher

Ah, sorry to hear it. I did find this comment to a post about debugging with Tampermonkey. It's a little older, so I don't know if it's still relevant but it might be worth a try. If all else fails, the post's answer just uses alert() to see what values you're working with in your script. Hopefully the other debugging tool works though. Good luck!