Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External dependency not defined, even though it was loaded with @require

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.

console error

// ==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'
    );
}
like image 394
Moist Nugget Avatar asked Oct 21 '25 07:10

Moist Nugget


1 Answers

<!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 // @unwrap to 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!

like image 134
Mehdi Avatar answered Oct 23 '25 20:10

Mehdi