Advertisement
nubideus

Untitled

Feb 26th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function applyScriptToTabHead(tab, source, callback) {
  2.     var isInterrupted = false;
  3.  
  4.     var code = `
  5.             (() => {
  6.                 if(document.head) {
  7.                     var i = document.createElement("script");
  8.                     i.innerHTML = decodeURI(\"${encodeURI(source)}\");
  9.                    document.head.appendChild(i);
  10.                }
  11.                return !!document.head;
  12.            })();
  13.            `;
  14.    var loop = () => {
  15.        chrome.tabs.executeScript(tab.id, {
  16.            code: code,
  17.            runAt: "document_start"
  18.        }, function(args) {
  19.            if (isInterrupted) {
  20.                return;
  21.            }
  22.            if (!args[0]) {
  23.                setTimeout(loop, 1);
  24.            } else {
  25.                callback();
  26.            }
  27.        });
  28.    };
  29.    loop();
  30.  
  31.    return {
  32.        tab: tab,
  33.        disable: () => {
  34.            isInterrupted = true;
  35.        }
  36.    };
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement