Question

One-click downloading with bookmarklets

  • 5 February 2018
  • 8 replies
  • 374 views

Userlevel 7
Badge

Disclaimer: Since this approach refers to elements in Looker’s DOM, it can’t be guaranteed to work for any particular future version. That said, it would probably be easy to update!


If you’d like to hard-code certain download options and skip past the download modal, you can leverage a bookmarklet and some knowledge of Looker’s URL structure to do so.


First, here’s the raw code:


var exploreLocation, downloadUrl
if(document.location.pathname.match(/^\/explore\//)){
exploreLocation = document.location
}
if(document.location.pathname.match(/^\/looks\//)){
exploreLocation = document.querySelector(".link-bar a[href^='/explore']")
}
if(document.location.pathname.match(/^\/dashboards\/\d+$/)){
downloadUrl =
document.location.pathname
+ "/downloadzip"
+ "?"
+ (document.location.search.slice(1).split("&").filter(s=>s.indexOf("filter_config=")==0)[0]||"").replace(/^filter_config/,"filters")
}
if(exploreLocation){
var downloadUrl =
exploreLocation.pathname
+ ".csv"
+ exploreLocation.search
+ "&apply_formatting=true&apply_vis=true&limit=-1&download=yes";

}
if(downloadUrl){
document.location.href=downloadUrl
}else{
alert("No download logic available for this type of page")
}

And here’s that code with linebreaks removed to make it easier to copy in to a bookmarklet:


javascript: var exploreLocation, downloadUrl; if(document.location.pathname.match(/^\/explore\//)){ exploreLocation = document.location } if(document.location.pathname.match(/^\/looks\//)){ exploreLocation = document.querySelector(".link-bar a[href^='/explore']") } if(document.location.pathname.match(/^\/dashboards\/\d+$/)){ downloadUrl = document.location.pathname + "/downloadzip" + "?" + (document.location.search.slice(1).split("&").filter(s=>s.indexOf("filter_config=")==0)[0]||"").replace(/^filter_config/,"filters") } if(exploreLocation){ var downloadUrl = exploreLocation.pathname + ".csv" + exploreLocation.search + "&apply_formatting=true&apply_vis=true&limit=-1&download=yes"; } if(downloadUrl){ document.location.href=downloadUrl }else{ alert("No download logic available for this type of page") }


8 replies

@fabio - This is super cool. I know nothing about how this works, but any chance it would be possible to modify the bookmarklet to download the PDF of a dashboard instead of a zip of CSV’s?

Userlevel 7
Badge +1

@jpklwr I edited this code for my own use and it works fine:


if(document.location.pathname.match(/^\/dashboards\/\d+$/)){
var today = new Date();
downloadUrl = "/render/process/wd/1680/1"
+ document.location.pathname
+ ".pdf"
+ "?download=yes&filename="
+ $(".title-main:first").text()
+ today.toISOString()
+ ".pdf&title="
+ $(".title-main:first").text()
+ "&longTables=false&pdf_paper_size=&pdf_landscape=false"
+ (document.location.search.slice(1).split("&").filter(s=>s.indexOf("filter_config=")==0)[0]||"").replace(/^filter_config/,"filters")
}

The only bit I’m not sure is 1680/1 whether it’s the same for each instance. @izzy could you confirm?

Userlevel 7
Badge +1

Sorry for the late reply, Dawid. I think that’s referring to a resolution of 1680x< something> and should be fine in this case-- You might find render URLs in the wild that are different width/height, but this just means you’re hardcoding the size of the downloaded pdf.


Looks good to me, but I’m by no means the last word on this.

Gimme a second to process here… but does that mean we can change the width of the downloaded PDF’s by manipulating the URL? This would be awesome, as they generally render distastefully wide

Userlevel 7
Badge +1

It looks that way! You can change that integer in the render URL to whatever you’d like and generate a dashboard of that width.

Userlevel 3
Badge

@Dawid_Nawrot Thanks for sharing this, it’s fantastic! Can I ask for your help, how can I edit the code to respect the filters applied when printing the pdf? Thanks!

Userlevel 7
Badge +1

Hi @dvd.p


I changed the code slightly, this will take the whole search part of the URL and move it to the bookmark:


if(document.location.pathname.match(/^\/dashboards\/\d+$/)){
var today = new Date();
downloadUrl = "/render/process/wd/1680/1"
+ document.location.pathname
+ ".pdf"
+ "?download=yes&filename="
+ $(".title-main:first").text()
+ today.toISOString()
+ ".pdf&title="
+ $(".title-main:first").text()
+ "&longTables=false&pdf_paper_size=&pdf_landscape=false"
+ (document.location.search.length > 0 ? "&" + (document.location.search.slice(1)) : "")

}
Userlevel 3
Badge

@Dawid_Nawrot Works like a charm, thanks! 😉 really appreciated!

Reply