Discussions » Development

Script not triggered - Hash in URL

§
Posted: 2017-04-14

Script not triggered - Hash in URL

Hi. I'm trying to write a script for the page https://www.studyblue.com/#flashcard/edit/19366545. The number at the end is randomly created.
So I'm using the include *://www.studyblue.com/#flashcard/edit/*
This is not working. Thinking the hash is the problem I switched to *://www.studyblue.com/*flashcard/edit/*
It is not working. Any ideas?

wOxxOmMod
§
Posted: 2017-04-14

Hash-navigation means the page wasn't actually loaded normally from server, it was a so-called "AJAX-navigation". The scripts are auto-injected only during normal page load.

You'll need to inject it on the entire server *://www.studyblue.com/*. The first time your script runs is the normal page load so you need to check location.hash to decide whether it's a page you want to process. Then you'll need to find a way to catch the moment a hash-navigation occurs. The simplest method is to check in setInterval. Depending on the site you may attach an event listener that will be triggered by the site code. For example, here's how my last.fm script forwards the site event to my main script:

window.addEventListener('load', function onLoad() {
    window.removeEventListener('load', onLoad);
    unsafeWindow.jQuery(unsafeWindow.document).on('pjax:end', exportFunction(relayPJAX, unsafeWindow));
});

function relayPJAX() {
    document.dispatchEvent(new CustomEvent('pjax:end.listeners-you-know'));
}

document.addEventListener('pjax:end.listeners-you-know', process);

function process() {
    const link = document.querySelector('a[href*="/+listeners/you-know"]');
    if (!link)
        return;
    ................
}

You can see which events are defined by the site by running getEventListeners(document) or getEventListeners(window) in devtools console.

Post reply

Sign in to post a reply.