Copy text from a webpage using Javascript

Actually, I made some progress...I was able to get the following to work in the console. I just copied the JS path of the element I am looking for and appended it with ".innerText":

document.querySelector("#viewer").shadowRoot.querySelector("#toolbar").shadowRoot.querySelector("#title").innerText

Now the problem is getting it to work in KM. Based on the thread below, I thought adding "return" at the beginning would help, but the variable I am saving to does not get filled. I am using KM 11.0.3 if that helps. Any thoughts on this?