0

I'm working on a userscript for a D2L/Brightspace page, and part of what it does is modify the list of courses that a user is enrolled in.

The problem is when you click on the icon to open the list of courses, it makes an XHR request to a different page to get the list, and then updates the element on the page, so I have to wait for it to load before modifying the list in any way.

What I'm doing currently is making a setTimeout(function() {}, 500); function to wait half a second before making any modifications, but that doesn't work 100% of the time. Sometimes the internet is slower and it takes the XHR request more than half a second to load, so it gets modified before it exists, meaning my changes do not happen. Other times, the internet is fast, and the old list is shown for about half a second, and then it's changed, which doesn't look nice.

I would like to be able to, instead of using a setTimeeout() function, use something that allows me to run a function immediately after the XHR request has successfully loaded.

The XHR request is sent to https://d2l.ca/d2l/lp/courseSelector/440/InitPartial?...*. Is there any way that I could track that specific one and run something when it's completed?

I have tried .ajaxStop(), but since this isn't an ajax request, something else is needed.

Thanks :)

*Link modified slightly for anonymity.

RedXTech
  • 406
  • 2
  • 7
  • 17
  • Post your XHR code, please. This is straight forward – Adam Azad Dec 09 '16 at 17:51
  • @AdamAzad I don't have the XHR code. That's on D2L. I just want to track the XHR request to the specifc URL. – RedXTech Dec 09 '16 at 17:52
  • How is `XMLHttpRequest` called? Can you include `html` and current `setTimeout` call at Question? – guest271314 Dec 09 '16 at 17:55
  • @guest271314 Here's a link to my setTimeout request: http://hastebin.com/cuyusivoki.js . What do you mean by the html? Do you want to see the HTML of the entire page? Or just the element that I click, or the one that I'm updating? – RedXTech Dec 09 '16 at 18:03
  • Can you include relevant `html` releated to Question and `javascript` at actual text of Question? See http://stackoverflow.com/help/how-to-ask, http://stackoverflow.com/help/mcve – guest271314 Dec 09 '16 at 18:04
  • @guest271314 You see that's the problem. The page is loading over 40 different very large javascript files, and I do not know which one is calling the XHR. – RedXTech Dec 09 '16 at 18:06
  • @RedXTech You can use `ajaxStop` event. See http://stackoverflow.com/questions/38166563/how-can-i-know-when-ajax-calls-are-finished-with-getscript – guest271314 Dec 09 '16 at 18:13
  • @guest271314 That isn't working for me. I suppose that it isn't an ajax call being used? Could you un-mark as duplicate please? It's a different situation. – RedXTech Dec 09 '16 at 19:09
  • @RedXTech Yes. Can you update Question describing the issue and requirement? – guest271314 Dec 09 '16 at 19:14
  • @guest271314 is that good? – RedXTech Dec 09 '16 at 19:16
  • @RedXTech How do you know that an `XMLHttpRequest` is being made? – guest271314 Dec 09 '16 at 19:17
  • @guest271314 this shows up when I click on the menu - https://gyazo.com/5e107a1011be17d3c4deae4e4999207f – RedXTech Dec 09 '16 at 19:19
  • @RedXTech Nothing is displayed at link. – guest271314 Dec 09 '16 at 19:23
  • @guest271314 That's not the entirety of the link. I cut it off before because there's personal information on there in realtion to my courses at school, etc. https://gyazo.com/bf06c106fdcf96f6d18b76875c74c06f that's the first part of it. – RedXTech Dec 09 '16 at 19:24
  • Nothing is displayed at link. _"The page is loading over 40 different very large javascript files, and I do not know which one is calling the XHR."_ Have you checked the text of the `.js` files to determine where `XMLHttpRequest()` is called? – guest271314 Dec 09 '16 at 19:27
  • @guest271314 line 37 of this file - https://dl.cssd.ab.ca/d2l/common/js/D2L.LP.Web.Desktop.default.js?v=10.6.7.4474-167 – RedXTech Dec 09 '16 at 19:28

0 Answers0