Several common problems and tasks you will encounter while writing a userscript have already been solved. You may consider adding a dependency to a library like WazeWrap in your userscript so you don't need to investigate these yourself any more. This can be done by referring to them in the script header:
// ==UserScript== // ... // @require https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js // ==/UserScript==