Skip to content

Instantly share code, notes, and snippets.

@embeddinglayer
Created July 11, 2024 19:35
Show Gist options
  • Select an option

  • Save embeddinglayer/1d9a1b6ba669387d9f6327dd2693cc23 to your computer and use it in GitHub Desktop.

Select an option

Save embeddinglayer/1d9a1b6ba669387d9f6327dd2693cc23 to your computer and use it in GitHub Desktop.
function detectTamperMonkey(callback) {
/**
* Detect tampermonkey via stack dumping
*/
const createElem = document.createElement;
document.createElement = function() {
try {
throw new Error();
} catch (e) {
const stackTrace = e.stack;
if (stackTrace.includes('userscript.html')) {
callback(stackTrace, arguments[0]);
return;
}
}
return createElem.apply(this, arguments);
};
}
// Usage
/*
detectTamperMonkey((stackTrace, element) => {
console.log('TamperMonkey detected!');
window.location.href = 'about:blank';
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment