While working on simonw/datasette-leaflet-freedraw#10 I found myself writing this atrocity to figure out the SQL query used for a specific table page:
let innerSql = Array.from(document.getElementsByTagName("span")).filter(
el => el.innerText == "View and edit SQL"
)[0].parentElement.getAttribute("title")
This is obviously bad - it's very brittle, and will break if I ever change the text on that link (like localizing it for example).
Instead, I think pages like that one should have a block of script at the bottom something like this:
window.datasette = window.datasette || {};
datasette.view_name = 'table';
datasette.table_sql = 'select * from ...';