I am trying to install and use sweetalert2 in Tampermonkey but I am getting:
Swal is undefined
in the console.
I tried to use @require and /* globals Swal */ but it didn't work.

// ==UserScript==
// @name Quizlet Explanations Get Answer
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://quizlet.com/explanations/questions/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=quizlet.com
// @require https://cdn.jsdelivr.net/npm/[email protected]/dist/sweetalert2.all.min.js
// @grant none
// ==/UserScript==
/* global Swal */
window.onload = function() {
Swal.fire(
'Good job!',
'You clicked the button!',
'success'
);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>sweetalert2 example</title>
</head>
<body>
<script>
window.onload = function() {
Swal.fire({
title: 'Good job!',
text: 'You clicked the button!',
type: 'success',
});
}
</script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
</body>
</html>
Link it to this URL instead https://cdn.jsdelivr.net/npm/sweetalert2@11
// @require https://cdn.jsdelivr.net/npm/sweetalert2@11
As double-beep has mentioned previously you need to:
Enable sandbox mode by adding
// @unwrapto the headers
and leave it empty.
// @unwrap
Then call your modal like this:
/* global Swal */
window.onload = function() {
Swal.fire({
title: 'Good job!',
text: 'You clicked the button!',
type: 'success',
});
}
I hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With