Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

waitForUrl calls the function with the old body #16

Open
TheBestPessimist opened this issue Dec 1, 2021 · 3 comments
Open

waitForUrl calls the function with the old body #16

TheBestPessimist opened this issue Dec 1, 2021 · 3 comments

Comments

@TheBestPessimist
Copy link

TheBestPessimist commented Dec 1, 2021

My scripts here: https://github.com/TheBestPessimist/UserScripts/blob/master/youtube-auto-dislike/youtube-auto-dislike.user.js#L36-L43

I am calling waitForUrl(waitForElems()), and waitForElems is called before the content of the page is updated.
It looks as if waitForUrl is "too fast".

Am I using this wrong (I am a Kotlin/Backend dev)?
Is this expected?
Do you have any advice if what I did there is OK?

@fuzetsu
Copy link
Owner

fuzetsu commented Dec 6, 2021

Strange, I haven't hit this issue before. What error are you getting? Is it that document.body isn't available yet?
I mainly use violentmonkey and I haven't hit this, may have to do with how/when the script runner injects the script.

One option may be to change when your script runs, https://www.tampermonkey.net/documentation.php#_run_at
For example using document-body

Also looking at your code you may not need waitForUrl at all. If you don't pass stop:true to waitForElems it will just keep looking for new matches to that element, so even without waitiing for the url it should keep picking the new button as it appears.

@TheBestPessimist
Copy link
Author

TheBestPessimist commented Dec 6, 2021

I'm not getting errors afais, it's just that when i click a new video from the suggested on the right, the userscript is run IMMEDIATELY but the html elements it finds are the old ones.
That's why my solution is to sleep 1 second

I'm already using violentmonkey.

@TheBestPessimist
Copy link
Author

Regarding the "use only wait for elems with stop=false" advice: this doesnt work.

For the code below, do the following: un-dislike a yt video and wait 1 second. the video is not disliked again. no logs are written to console either. (logs should be written no matter what)

const util = {
    log: (...args) => console.log(`%c${SCRIPT_NAME}:`, "font-weight: bold;color: green;", ...args)
};

const SCRIPT_NAME = "YouTube Auto Dislike";

const CSS = {
    like: "ytd-video-primary-info-renderer #top-level-buttons-computed > ytd-toggle-button-renderer:nth-child(1)",
    dislike: "ytd-video-primary-info-renderer #top-level-buttons-computed > ytd-toggle-button-renderer:nth-child(2)",
    buttonClicked: "style-default-active"
}


waitForElems({ 
                sel: CSS.like,
                onmatch: dislike,
                stop: false,
                throttle: 1000,
            });


function dislike(btn) {
    util.log("begin debug")
    util.log(btn)

    const likeButton = document.querySelector(CSS.like)
    const dislikeButton = document.querySelector(CSS.dislike)

    if (likeButton.classList.contains(CSS.buttonClicked)) {
        util.log("Video is Liked by user => Nothing to do here. Will not dislike.")
        return
    }

    if (dislikeButton.classList.contains(CSS.buttonClicked)) {
        util.log("Video is already Disliked => Nothing to do here.")
        return
    }

    util.log("Will Dislike video.")
    dislikeButton.click()
    util.log("end debug")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants