Get Script

Getting scripts in an easier way.

  1. // ==UserScript==
  2. // @name Get Script
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.0
  5. // @description Getting scripts in an easier way.
  6. // @author cc
  7. // @include *
  8. // ==/UserScript==
  9.  
  10. (function() {
  11. 'use strict';
  12. var initialized = false;
  13. function initSecurity() {
  14. let meta = document.createElement('meta');
  15. meta.setAttribute('http-equiv', 'Content-Security-Policy');
  16. meta.setAttribute('content', 'upgrade-insecure-requests');
  17. document.head.appendChild(meta);
  18. initialized = true;
  19. }
  20. function getScript(url, callback) {
  21. if (url.startsWith('http:') && !initialized) {
  22. initSecurity();
  23. }
  24. let script = document.createElement('script');
  25. script.src = url;
  26. script.async='async';
  27. script.type='text/javascript';
  28. document.body.appendChild(script);
  29. if (typeof callback == 'function') {
  30. script.onload = callback;
  31. }
  32. }
  33. window.getScript = getScript;
  34. })();