Getting complete content of long list to script without scrolling
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
This message is written from the perspective of automating Canvas tasks using browser extensions.
In Canvas, the contents of some long lists (like People or Pages) are not displayed all at once, but pice by piece, adding more items when the user scrolls down to the (temporary) bottom of the incomplete list, a bit like in a Facebook feed. That behaviour introduces problems for scripts, as there seems to be no stated indication of how many items the list will eventually have. I have resorted to simulation of scrolling using Javascript's element.scrollIntoView() method. However, assumptions must then be made about how long time a scrolling event will take and how long/short time one should wait before concluding that yet another element.scrollIntoView() call won't produce more items, ie that the list has reached its true end. That is unsatisfactory, as the time needed may vary between computers.
I would like to know if there is a way around this. Ideally, the full content should be available by some procedure like AJAX (which doesn't work in my hands here since the output is only a page skeleton which I guess is then filled in by scripts) without the need to simulate user actions. Alternatively, if there were some indication at the top of the page as to how many items to expect from the full list, the script would at least know when to stop simulating scrolling.
Below is the function I have been using to automate scrolling of a <table> element using element.scrollIntoView(). I have set the delay parameter to 200 ms with success. dfd is a jQuery $.Deferred() variable defined outside of the function and is used to delay execution of subsequent steps until scrolling is complete. It works on my machine, but delay might require tuning by the user on another computer.
Ideas?
Best,
Dan
function scrollDownTable(obj,dfd,delay){
var oldBottom = 0;
var intv = setInterval(function(){
var lastline = $(obj).find("tr:last").get(0);
if(lastline != undefined){
lastline.scrollIntoView();
var newBottom = $(obj).find("tr").length;
setTimeout(function(){
if(newBottom == oldBottom){
clearInterval(intv);
dfd.resolve();
} else {oldBottom = newBottom;}
},delay);
}
},delay);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I actually found a useful method by playing around in DevTools a little. Using the course Id (here "2873600") and assuming that the number of pages is < 100, the following url will return a JSON string listing all pages:
https://canvas.instructure.com/api/v1/courses/2873600/pages?sort=title&order=asc&per_page=100
The output will look like this:
[{"title":"Ersättning för föreläsningar","created_at":"2021-05-23T16:15:46Z","url":"ersattning-for-forelasningar","editing_roles":"teachers","page_id":13599111,"last_edited_by":{"id":25064879,"display_name":"Dan Baeckström","avatar_image_url":"https://canvas.instructure.com/images/messages/avatar-50.png","html_url":"https://canvas.instructure.com/courses/1779153/users/25064879","pronouns":null},"published":false,"hide_from_students":true,"front_page":false,"html_url":"https://canvas.instructure.com/courses/1779153/pages/ersattning-for-forelasningar","todo_date":null,"updated_at":"2021-05-23T16:15:46Z","locked_for_user":false},{"title":"Exempel på frågetyper som förekommer resp inte förekommer på tentan","created_at":"2021-05-23T16:17:00Z","url":"exempel-pa-fragetyper-som-forekommer-resp-inte-forekommer-pa-tentan","editing_roles":"teachers","page_id":13599115,"last_edited_by":{"id":25064879,"display_name":"Dan Baeckström","avatar_image_url":"https://canvas.instructure.com/images/messages/avatar-50.png","html_url":"https://canvas.instructure.com/courses/1779153/users/25064879","pronouns":null},"published":false,"hide_from_students":true,"front_page":false,"html_url":"https://canvas.instructure.com/courses/1779153/pages/exempel-pa-fragetyper-som-forekommer-resp-inte-forekommer-pa-tentan","todo_date":null,"updated_at":"2021-05-23T16:17:00Z","locked_for_user":false}, {etc}]
From this, the actual number of items may also be deduced with a little JSON fiddling.
If the number of pages is >100, data will have to be retrieved in batches of up to 100 items each using pagination like this:
https://canvas.instructure.com/api/v1/courses/2873600/pages?page=1&sort=title&order=asc&per_page=100
https://canvas.instructure.com/api/v1/courses/2873600/pages?page=2&sort=title&order=asc&per_page=100
until the returned string is empty. For a more sophisticated solution, see @James 's post below.
//D