fonol / anki-search-inside-add-card

An add-on providing full-text-search and PDF reading functionality to Anki's Add card dialog
https://ankiweb.net/shared/info/1781298089
GNU Affero General Public License v3.0
179 stars 24 forks source link

Cannot Open Youtube Video #224

Open HQYang1979 opened 3 years ago

HQYang1979 commented 3 years ago

image

I am sure my connection is fine though. image this is the console message, but it seems not related.

fonol commented 3 years ago

Hm, the console output is not really helpful here. I rarely encounter problems with the Youtube player, in many cases an Anki restart helped. Does no video work? What is displayed on opening the video, any message?

HQYang1979 commented 3 years ago

No idea why, it suddenly works now! It's bugged me for three days. Thank you very much though.

You're really doing a great job.

HQYang1979 commented 3 years ago

image

it happens again. I can import the video, that means connection is fine, but just cannot play it.

fonol commented 3 years ago

Hi, anything in the console if that happens?

I can import the video, that means connection is fine

To create the video note, you don't have to have a working internet connection, only to actually watch.

HQYang1979 commented 3 years ago

There's nothing shown on the console...sometime it works sometime it wont. said it is youtube api problem as in the above screenshot.

andrewcrook commented 3 years ago

@fonol I have a similar bug to @HQYang1979 most of the time it displays a notification of the YouTube video's title but sits there with a blank screen. Rarely do I get a the YouTube API/internet connection warning.

Then some times after repeatedly trying it plays the video.

I put some extra logging into the python backend source I couldn't see any issues, so I thought it was the javascript. So I used the Anki add-on AnkiWebView Inspector (31746032) to open up a JS console for the webview on the add screen whilst opening a video note.

From the console I believe that the issue is caused by trying to use the YT Player before it has time to download and initialise.

Screenshot 2021-03-15 at 20 02 41

as you can see later I was able to reference YT and YT.Player meaning that they had loaded eventually.

The YT Player API reference shows how to asynchronously load the API code it also mentions a callback function called onYouTubeIframeAPIReady which is called after the YT API has downloaded.

Any web page that uses the IFrame API must also implement the following JavaScript function:

onYouTubeIframeAPIReady – The API will call this function when the page has finished downloading the JavaScript for the player API, which enables you to then use the API on your page. Thus, this function might create the player objects that you want to display when the page loads.

example from the page

// 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
          videoId: 'M7lc1UVf-VE',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
      }

if you wish you can then create the event callbacks.

onPlayerReady for when the video is ready in the player. onStateChange

So I wonder if moving some of the code into these callbacks and a few changes would resolve this issue?

fonol commented 3 years ago

Thanks, it really sounds like it could be the issue. Although I wonder, if you open the Add dialog, and open a note in the reader, the API should have had plenty of time to load by then. Maybe the problem only occurs if you open a Youtube note with something like ctrl+o, where the Add dialog was not previously open, so that there is little time between the script loading and opening the note in the reader? Anyway, making sure that the async loading has finished makes sense and I'll definitely include it with the next update.

andrewcrook commented 3 years ago

where the Add dialog was not previously open, so that there is little time between the script loading and opening the note in the reader?

yes I think that's what is happening

Add dialog, and open a note in the reader, the API should have had plenty of time to load by then.

Yes, that seems to be the case. I do occasionally get a YouTube page saying something went wrong.. not sure what that is at the moment.

I was opening from the queue screen so the browser screen had not loaded before. Different workflows and different internet speeds would explain why it isn't an issue for everyone.

Anyway, making sure that the async loading has finished makes sense and I'll definitely include it with the next update.

I think that's probably the right thing to do long-term.

Is it also possible to use caching and Etags saving on downloading API every time?

andrewcrook commented 3 years ago

I do occasionally get a YouTube page saying something went wrong.. not sure what that is at the moment.

Not seen the YouTube error screen mentioned since.

OQHan commented 3 years ago

image

andrewcrook commented 3 years ago

@OQHan

There are several ways to open video notes which are you using?

fonol commented 3 years ago

@OQHan that's a bummer. How often does that still happen, and if it happens, still nothing suspicious in the console?

OcileCollection commented 2 years ago

@HQYang1979 Have you tried the new update? Did it solve the issue?