討論 » 建立請求

a script that automatically add app=desktop? on youtube video link

§
發表於:2023-04-27

I found that to make YouTube play on my Firefox android browser in the background on my new tablet do I need to add app=desktop& after watch? and before the actual YouTube video URL details.

Example: https://m.youtube.com/watch?v=23xt3x-mtY0

Becomes: https://m.youtube.com/watch?app=desktop&v=23xt3x-mtY0

I Use Firefox to avoid the ads plus I have the app removed, so can anyone help with a script that automatically add that line to every video or video in a playlist automatically via tampermonkey or some other way?

Before you suggest the toggle desktop sadly sometimes YouTube when playing in the background stops and after that does that tab not work but keeping it on mobile version adding that and it runs perfectly.

As you can see on my private playlist there is no m.youtube but pure www.youtube so the script should know to work with both.

My private playlist was: https://www.youtube.com/watch?v=_YQ63hOlF00&list=PL99_34VEiVAQy1vRf7ttJmad7E-6457sN&index=5

My private playlist becomes: https://www.youtube.com/watch?app=desktop&v=_YQ63hOlF00&list=PL99_34VEiVAQy1vRf7ttJmad7E-6457sN&index=5

Searching for a way to do this simple even searched for a tampermonkey script but what I found weren't what I needed or I don't know how to change it to my need, closes thing I found was on this page: https://stackoverflow.com/questions/10675049/add-parameters-to-the-url-redirect-via-a-greasemonkey-tampermonkey-userscript

I would really appreciate if anyone could write me a safe script that makes this automatic addition so I can just listen to my music without having to manually do it for each video.

NotYou管理員
§
發表於:2023-04-28

There you go:

// ==UserScript==
// @name        YouTube Desktop App
// @description Add ?app=desktop after page loading in YouTube.
// @match       *://*.youtube.com/*
// @match       *://youtube.com/*
// @license     Unlicense
// @run-at      document-start
// @grant       none
// ==/UserScript==

(function() {
  const url = new URL(location)
  const searchParams = url.searchParams

  if(!searchParams.has('app')) {
    searchParams.set('app', 'desktop')
    location.replace(url)
  }
})()
§
發表於:2023-04-28

There you go:

// ==UserScript==
// @name        YouTube Desktop App
// @description Add ?app=desktop after page loading in YouTube.
// @match       *://*.youtube.com/*
// @match       *://youtube.com/*
// @license     Unlicense
// @run-at      document-start
// @grant       none
// ==/UserScript==

(function() {
  const url = new URL(location)
  const searchParams = url.searchParams

  if(!searchParams.has('app')) {
    searchParams.set('app', 'desktop')
    location.replace(url)
  }
})()

Just to be sure this ads app=desktop& on YouTube after watch? and before the actually video link and also doesn't keep adding if the page reloads?
I'm very new to this so I just thought I ask before I install it.

NotYou管理員
§
發表於:2023-04-28
編輯:2023-04-28

Just to be sure this ads app=desktop& on YouTube after watch?

No, it adds add=desktop before at page loading.

and before the actually video link and also doesn't keep adding if the page reloads?

If I understood you correctly, then yes, it won't infinitely add app=desktop.

I'm very new to this so I just thought I ask before I install it.

No need to worry 😎, I used method that are working 100% fine.

§
發表於:2023-04-28

Just to be sure this ads app=desktop& on YouTube after watch?

No, it adds add=desktop before at page loading.

and before the actually video link and also doesn't keep adding if the page reloads?

If I understood you correctly, then yes, it won't infinitely add app=desktop.

I'm very new to this so I just thought I ask before I install it.

No need to worry 😎, I used method that are working 100% fine.

Awesome I will test it out as soon I can.

Just to be sure I install by going on my tablets Firefox, go into the settings of greasemonkey and copy that in as a script or something like that? I haven't done scripts for ages last time I did it where there a install button on the homepage for it, back in the day when you needed a script to avoid detection of ublock origin.

NotYou管理員
§
發表於:2023-04-28
編輯:2023-04-28

Just to be sure I install by going on my tablets Firefox, go into the settings of greasemonkey and copy that in as a script or something like that? I haven't done scripts for ages last time I did it where there a install button on the homepage for it, back in the day when you needed a script to avoid detection of ublock origin.

Yeah, just copy-paste it. There is no install button because this isn't a script page, just the code lines. Don't know about the earlier time, but now you don't need script to avoid detection of uBlock Origin (at least it's fine for me, I'm using uBG daily and didn't meet that "bug" yet).

§
發表於:2023-04-28

Just to be sure I install by going on my tablets Firefox, go into the settings of greasemonkey and copy that in as a script or something like that? I haven't done scripts for ages last time I did it where there a install button on the homepage for it, back in the day when you needed a script to avoid detection of ublock origin.

Yeah, just copy-paste it. There is no install button because this isn't a script page, just the code lines. Don't know about the earlier time, but now you don't need script to avoid detection of uBlock Origin (at least it's fine for me, I'm using uBG daily and didn't meet that "bug" yet).

No I know the bug that required the script was fixed I was trying to say last time I had to use a script and tampermonkey was when it was needed to avoid homepages detecting ublock. And I just tested it and it works perfect thank you so much for it, amazing easy if I actually understood it.

It did complain that there was no version number so I just added that and it stopped giving me a warning sign hope that's ok?

§
發表於:2023-04-28

Okay a little thing when next video in my playlist loads automatically is it without the desktop added I have to reload to get it added, I will test some more

NotYou管理員
§
發表於:2023-04-29

... hope that's ok?

Yeah, that's ok, adding meta values won't do anything critical to script, especially those, which are not affecting script functional.

Okay a little thing when next video in my playlist loads automatically is it without the desktop added I have to reload to get it added, I will test some more

That's because YouTube is SPA (Single Page Application), which means that the page doesn't actually reload, it just loads content from the server. So instead of page reloading, the website updates itself content, that's why the script doesn't work. I probably can observe changes using PerformanceObserver, but that may cause performance issues. There is solution with popstate event, I don't know if it would work, but I think you can give it a try:

// ==UserScript==
// @name        YouTube Desktop App
// @description Add ?app=desktop after page loading in YouTube.
// @version     1.0.0
// @match       *://*.youtube.com/*
// @match       *://youtube.com/*
// @license     Unlicense
// @run-at      document-start
// @grant       none
// ==/UserScript==

(function() {
  addParameter()

  window.addEventListener('popstate', addParameter)

  function addParameter() {
    const url = new URL(location)

    return changeURL(url)
  }

  function changeURL(url) {
    const searchParams = url.searchParams

    if(!searchParams.has('app')) {
      searchParams.set('app', 'desktop')

      return location.replace(url)
    }

    return void 0
  }
})()
§
發表於:2023-04-29

... hope that's ok?

Yeah, that's ok, adding meta values won't do anything critical to script, especially those, which are not affecting script functional.

Okay a little thing when next video in my playlist loads automatically is it without the desktop added I have to reload to get it added, I will test some more

That's because YouTube is SPA (Single Page Application), which means that the page doesn't actually reload, it just loads content from the server. So instead of page reloading, the website updates itself content, that's why the script doesn't work. I probably can observe changes using PerformanceObserver, but that may cause performance issues. There is solution with popstate event, I don't know if it would work, but I think you can give it a try:

// ==UserScript==
// @name        YouTube Desktop App
// @description Add ?app=desktop after page loading in YouTube.
// @version     1.0.0
// @match       *://*.youtube.com/*
// @match       *://youtube.com/*
// @license     Unlicense
// @run-at      document-start
// @grant       none
// ==/UserScript==

(function() {
  addParameter()

  window.addEventListener('popstate', addParameter)

  function addParameter() {
    const url = new URL(location)

    return changeURL(url)
  }

  function changeURL(url) {
    const searchParams = url.searchParams

    if(!searchParams.has('app')) {
      searchParams.set('app', 'desktop')

      return location.replace(url)
    }

    return void 0
  }
})()

I will try this any chance to make it automatically start a video, what happens for some strange reason on my tablet only no idea why is while in other apps or tabs the page seems to reload out of the blue forcing me to go back and start the music again from whatever app I am in.

No sure if the video actually unloads completely until I go back into Firefox making my request moot, I really have no idea why on my tablet with same Android OS and settings for Firefox do I not have on m.YouTube or app=desktop this little notification area showing what is playing and can force it to play, which I do have on my phone, this is why I am here for this script, only way I can make the tablet play in the background unlike my phone is via the desktop part in m. will it not play in background at all.

發表回覆

登入以回復