clickio

Вставка скрипта в начало body из js

Jan 26th, 2021 (edited)
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function (d){
  2.     let b = d.querySelector('body');
  3.     let target = document;
  4.     let observer = null;
  5.  
  6.     const config = {
  7.         attributes: true,
  8.         childList: true,
  9.         subtree: true
  10.     };
  11.    
  12.     const observerCallback = function(mutationsList, observer) {
  13.         b = d.querySelector("body");
  14.         for (let mutation of mutationsList) {
  15.             console.log(mutation);
  16.             if (b && mutation.target === b && !d.querySelector("body > script[src=https://relap.io/v7/relap.js]"))
  17.             {
  18.                 addScript();
  19.                 observer.disconnect();
  20.             }
  21.         }
  22.     };
  23.    
  24.     const addScript = function() {
  25.         let s = d.createElement("script");
  26.         s.setAttribute('async', 'async');
  27.         s.setAttribute('type', 'text/javascript');
  28.         s.setAttribute('src', 'https://relap.io/v7/relap.js');
  29.         s.setAttribute('data-relap-token', '45vjvDi8uv8CO87L');
  30.         b.insertBefore(s, d.querySelector('body > :first-child'));
  31.     };
  32.    
  33.     if(b)
  34.     {
  35.         addScript();
  36.     }
  37.     else
  38.     {
  39.         observer = new MutationObserver(observerCallback);
  40.         observer.observe(target, config);
  41.     }
  42. })(document);
Add Comment
Please, Sign In to add comment