Skip to content

Instantly share code, notes, and snippets.

@rivy
Forked from mathiasbynens/unsafeWindow.user.js
Created January 1, 2018 17:17
Show Gist options
  • Save rivy/5695a794d0fc076731834e9b19967c7d to your computer and use it in GitHub Desktop.
Save rivy/5695a794d0fc076731834e9b19967c7d to your computer and use it in GitHub Desktop.
`unsafeWindow` polyfill (for use in user scripts)
// ==UserScript==
// @name Emulate `unsafeWindow` in browsers that don’t support it.
// ==/UserScript==
// http://mths.be/unsafewindow
window.unsafeWindow || (
unsafeWindow = (function() {
var el = document.createElement('p');
el.setAttribute('onclick', 'return window;');
return el.onclick();
}())
);
// You can now use `unsafeWindow`, ehm, safely.
console.log(unsafeWindow);
// If the current document uses a JavaScript library, you can use it in
// your user script like this:
console.log(unsafeWindow.jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment