How to call one function after another in Chrome Extension content script











up vote
0
down vote

favorite












I have two functions injectChat and firstTimeTrigger in my content script. Both of them attach a script to the body of the DOM.



I need injectChat to run first and after it's fully complete then load firstTimeTrigger. firstTimeTrigger doesn't work unless injectChat run and is fully loaded.



These are the two functions -



function injectChat(){
console.log("Injecting Chat");

const script = document.createElement('script');
script.setAttribute("type","text/javascript");
script.setAttribute("src", `${host}/api/botpress-platform-webchat/inject.js/`);
script.setAttribute("id", 'botpress-script');
document.body.appendChild(script);

script.addEventListener('load', function(){
const botpress_settings = `window.botpressWebChat.init({host: '${host}'})`;
const settings = document.createElement('script');
settings.setAttribute("id", "botpress-settings");
settings.innerHTML = botpress_settings;
document.body.appendChild(settings);

});

};

function firstTimeTrigger(){
console.log("First Time Trigger");
chrome.runtime.sendMessage({type: "isFirstTime"}, function(response) {
if(response == true){
const botpress_trigger_1 = "window.botpressWebChat.sendEvent({ type: 'show' })";
const botpress_trigger_2 = `window.botpressWebChat.sendEvent({ type: 'proactive-trigger', platform: 'web', text: '${JSON.stringify(config)}' })`;
const trigger = document.createElement('script');
trigger.innerHTML = botpress_trigger_1 + 'n' + botpress_trigger_2;
document.body.appendChild(trigger);
}
});
};


Currently, I've been doing it like this



injectChat();
setTimeout(function(){
firstTimeTrigger();
}, 3000);


But it's very unreliable because of the various page load times due to this being inside a content script.



How do I make this happen? Promises don't work in here.










share|improve this question
























  • Looks like the firstTimeTrigger() function will trigger only once after the injectChat() completed. Why don't you merge firstTimeTrigger() at the end of injectChat() ?
    – elegant-user
    Nov 5 at 6:11










  • Yeah I wish. It just doesn't work
    – Nilay Modi
    Nov 5 at 6:27















up vote
0
down vote

favorite












I have two functions injectChat and firstTimeTrigger in my content script. Both of them attach a script to the body of the DOM.



I need injectChat to run first and after it's fully complete then load firstTimeTrigger. firstTimeTrigger doesn't work unless injectChat run and is fully loaded.



These are the two functions -



function injectChat(){
console.log("Injecting Chat");

const script = document.createElement('script');
script.setAttribute("type","text/javascript");
script.setAttribute("src", `${host}/api/botpress-platform-webchat/inject.js/`);
script.setAttribute("id", 'botpress-script');
document.body.appendChild(script);

script.addEventListener('load', function(){
const botpress_settings = `window.botpressWebChat.init({host: '${host}'})`;
const settings = document.createElement('script');
settings.setAttribute("id", "botpress-settings");
settings.innerHTML = botpress_settings;
document.body.appendChild(settings);

});

};

function firstTimeTrigger(){
console.log("First Time Trigger");
chrome.runtime.sendMessage({type: "isFirstTime"}, function(response) {
if(response == true){
const botpress_trigger_1 = "window.botpressWebChat.sendEvent({ type: 'show' })";
const botpress_trigger_2 = `window.botpressWebChat.sendEvent({ type: 'proactive-trigger', platform: 'web', text: '${JSON.stringify(config)}' })`;
const trigger = document.createElement('script');
trigger.innerHTML = botpress_trigger_1 + 'n' + botpress_trigger_2;
document.body.appendChild(trigger);
}
});
};


Currently, I've been doing it like this



injectChat();
setTimeout(function(){
firstTimeTrigger();
}, 3000);


But it's very unreliable because of the various page load times due to this being inside a content script.



How do I make this happen? Promises don't work in here.










share|improve this question
























  • Looks like the firstTimeTrigger() function will trigger only once after the injectChat() completed. Why don't you merge firstTimeTrigger() at the end of injectChat() ?
    – elegant-user
    Nov 5 at 6:11










  • Yeah I wish. It just doesn't work
    – Nilay Modi
    Nov 5 at 6:27













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have two functions injectChat and firstTimeTrigger in my content script. Both of them attach a script to the body of the DOM.



I need injectChat to run first and after it's fully complete then load firstTimeTrigger. firstTimeTrigger doesn't work unless injectChat run and is fully loaded.



These are the two functions -



function injectChat(){
console.log("Injecting Chat");

const script = document.createElement('script');
script.setAttribute("type","text/javascript");
script.setAttribute("src", `${host}/api/botpress-platform-webchat/inject.js/`);
script.setAttribute("id", 'botpress-script');
document.body.appendChild(script);

script.addEventListener('load', function(){
const botpress_settings = `window.botpressWebChat.init({host: '${host}'})`;
const settings = document.createElement('script');
settings.setAttribute("id", "botpress-settings");
settings.innerHTML = botpress_settings;
document.body.appendChild(settings);

});

};

function firstTimeTrigger(){
console.log("First Time Trigger");
chrome.runtime.sendMessage({type: "isFirstTime"}, function(response) {
if(response == true){
const botpress_trigger_1 = "window.botpressWebChat.sendEvent({ type: 'show' })";
const botpress_trigger_2 = `window.botpressWebChat.sendEvent({ type: 'proactive-trigger', platform: 'web', text: '${JSON.stringify(config)}' })`;
const trigger = document.createElement('script');
trigger.innerHTML = botpress_trigger_1 + 'n' + botpress_trigger_2;
document.body.appendChild(trigger);
}
});
};


Currently, I've been doing it like this



injectChat();
setTimeout(function(){
firstTimeTrigger();
}, 3000);


But it's very unreliable because of the various page load times due to this being inside a content script.



How do I make this happen? Promises don't work in here.










share|improve this question















I have two functions injectChat and firstTimeTrigger in my content script. Both of them attach a script to the body of the DOM.



I need injectChat to run first and after it's fully complete then load firstTimeTrigger. firstTimeTrigger doesn't work unless injectChat run and is fully loaded.



These are the two functions -



function injectChat(){
console.log("Injecting Chat");

const script = document.createElement('script');
script.setAttribute("type","text/javascript");
script.setAttribute("src", `${host}/api/botpress-platform-webchat/inject.js/`);
script.setAttribute("id", 'botpress-script');
document.body.appendChild(script);

script.addEventListener('load', function(){
const botpress_settings = `window.botpressWebChat.init({host: '${host}'})`;
const settings = document.createElement('script');
settings.setAttribute("id", "botpress-settings");
settings.innerHTML = botpress_settings;
document.body.appendChild(settings);

});

};

function firstTimeTrigger(){
console.log("First Time Trigger");
chrome.runtime.sendMessage({type: "isFirstTime"}, function(response) {
if(response == true){
const botpress_trigger_1 = "window.botpressWebChat.sendEvent({ type: 'show' })";
const botpress_trigger_2 = `window.botpressWebChat.sendEvent({ type: 'proactive-trigger', platform: 'web', text: '${JSON.stringify(config)}' })`;
const trigger = document.createElement('script');
trigger.innerHTML = botpress_trigger_1 + 'n' + botpress_trigger_2;
document.body.appendChild(trigger);
}
});
};


Currently, I've been doing it like this



injectChat();
setTimeout(function(){
firstTimeTrigger();
}, 3000);


But it's very unreliable because of the various page load times due to this being inside a content script.



How do I make this happen? Promises don't work in here.







javascript google-chrome-extension






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 5 at 4:48

























asked Nov 5 at 3:40









Nilay Modi

63




63












  • Looks like the firstTimeTrigger() function will trigger only once after the injectChat() completed. Why don't you merge firstTimeTrigger() at the end of injectChat() ?
    – elegant-user
    Nov 5 at 6:11










  • Yeah I wish. It just doesn't work
    – Nilay Modi
    Nov 5 at 6:27


















  • Looks like the firstTimeTrigger() function will trigger only once after the injectChat() completed. Why don't you merge firstTimeTrigger() at the end of injectChat() ?
    – elegant-user
    Nov 5 at 6:11










  • Yeah I wish. It just doesn't work
    – Nilay Modi
    Nov 5 at 6:27
















Looks like the firstTimeTrigger() function will trigger only once after the injectChat() completed. Why don't you merge firstTimeTrigger() at the end of injectChat() ?
– elegant-user
Nov 5 at 6:11




Looks like the firstTimeTrigger() function will trigger only once after the injectChat() completed. Why don't you merge firstTimeTrigger() at the end of injectChat() ?
– elegant-user
Nov 5 at 6:11












Yeah I wish. It just doesn't work
– Nilay Modi
Nov 5 at 6:27




Yeah I wish. It just doesn't work
– Nilay Modi
Nov 5 at 6:27












2 Answers
2






active

oldest

votes

















up vote
0
down vote













You can pass firstTimeTrigger inside of injectChat as a parameter and call it at the end of the function, like this:



function injectChat(firstTimeTrigger) {
// logic of the injectChat function...
firstTimeTrigger();
}





share|improve this answer





















  • That doesn't work because the function is called before the body of injectChat gets loaded.
    – Nilay Modi
    Nov 5 at 4:46


















up vote
0
down vote













Welcome to Javascript's callback hell :)



In order to run a function after the previous script has finished, you have to call it at the end of the script's load event. Remember to set up the load listener before actually adding the script element to the page. For example:



function injectChat(){
console.log('Injecting Chat');

const script = document.createElement('script');
script.setAttribute('type','text/javascript');
script.setAttribute('src', `${host}/api/botpress-platform-webchat/inject.js/`);
script.setAttribute('id', 'botpress-script');
script.addEventListener('load', injectSettings); //set up the load listener ...

document.body.appendChild(script); //...before adding the script element to the page
}

function injectSettings(){
console.log('Injecting settings');

const settings = document.createElement('script');
settings.setAttribute('id', 'botpress-settings');
settings.innerHTML = `window.botpressWebChat.init({host: '${host}'})`;
settings.addEventListener('load', firstTimeTrigger); //set up listener...

document.body.appendChild(settings); //...before injecting code
}

function firstTimeTrigger(){
console.log('First Time Trigger');
chrome.runtime.sendMessage({type: 'isFirstTime'}, function(response) {
if(response == true){

const trigger = document.createElement('script');
trigger.innerHTML = `
window.botpressWebChat.sendEvent({ type: 'show' });
window.botpressWebChat.sendEvent({ type: 'proactive-trigger', platform: 'web', text: ${JSON.stringify(config)} });
`;
document.body.appendChild(trigger);
}
});
}

injectChat();





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148007%2fhow-to-call-one-function-after-another-in-chrome-extension-content-script%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You can pass firstTimeTrigger inside of injectChat as a parameter and call it at the end of the function, like this:



    function injectChat(firstTimeTrigger) {
    // logic of the injectChat function...
    firstTimeTrigger();
    }





    share|improve this answer





















    • That doesn't work because the function is called before the body of injectChat gets loaded.
      – Nilay Modi
      Nov 5 at 4:46















    up vote
    0
    down vote













    You can pass firstTimeTrigger inside of injectChat as a parameter and call it at the end of the function, like this:



    function injectChat(firstTimeTrigger) {
    // logic of the injectChat function...
    firstTimeTrigger();
    }





    share|improve this answer





















    • That doesn't work because the function is called before the body of injectChat gets loaded.
      – Nilay Modi
      Nov 5 at 4:46













    up vote
    0
    down vote










    up vote
    0
    down vote









    You can pass firstTimeTrigger inside of injectChat as a parameter and call it at the end of the function, like this:



    function injectChat(firstTimeTrigger) {
    // logic of the injectChat function...
    firstTimeTrigger();
    }





    share|improve this answer












    You can pass firstTimeTrigger inside of injectChat as a parameter and call it at the end of the function, like this:



    function injectChat(firstTimeTrigger) {
    // logic of the injectChat function...
    firstTimeTrigger();
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 5 at 4:14









    Sergii Rudenko

    1,3961914




    1,3961914












    • That doesn't work because the function is called before the body of injectChat gets loaded.
      – Nilay Modi
      Nov 5 at 4:46


















    • That doesn't work because the function is called before the body of injectChat gets loaded.
      – Nilay Modi
      Nov 5 at 4:46
















    That doesn't work because the function is called before the body of injectChat gets loaded.
    – Nilay Modi
    Nov 5 at 4:46




    That doesn't work because the function is called before the body of injectChat gets loaded.
    – Nilay Modi
    Nov 5 at 4:46












    up vote
    0
    down vote













    Welcome to Javascript's callback hell :)



    In order to run a function after the previous script has finished, you have to call it at the end of the script's load event. Remember to set up the load listener before actually adding the script element to the page. For example:



    function injectChat(){
    console.log('Injecting Chat');

    const script = document.createElement('script');
    script.setAttribute('type','text/javascript');
    script.setAttribute('src', `${host}/api/botpress-platform-webchat/inject.js/`);
    script.setAttribute('id', 'botpress-script');
    script.addEventListener('load', injectSettings); //set up the load listener ...

    document.body.appendChild(script); //...before adding the script element to the page
    }

    function injectSettings(){
    console.log('Injecting settings');

    const settings = document.createElement('script');
    settings.setAttribute('id', 'botpress-settings');
    settings.innerHTML = `window.botpressWebChat.init({host: '${host}'})`;
    settings.addEventListener('load', firstTimeTrigger); //set up listener...

    document.body.appendChild(settings); //...before injecting code
    }

    function firstTimeTrigger(){
    console.log('First Time Trigger');
    chrome.runtime.sendMessage({type: 'isFirstTime'}, function(response) {
    if(response == true){

    const trigger = document.createElement('script');
    trigger.innerHTML = `
    window.botpressWebChat.sendEvent({ type: 'show' });
    window.botpressWebChat.sendEvent({ type: 'proactive-trigger', platform: 'web', text: ${JSON.stringify(config)} });
    `;
    document.body.appendChild(trigger);
    }
    });
    }

    injectChat();





    share|improve this answer

























      up vote
      0
      down vote













      Welcome to Javascript's callback hell :)



      In order to run a function after the previous script has finished, you have to call it at the end of the script's load event. Remember to set up the load listener before actually adding the script element to the page. For example:



      function injectChat(){
      console.log('Injecting Chat');

      const script = document.createElement('script');
      script.setAttribute('type','text/javascript');
      script.setAttribute('src', `${host}/api/botpress-platform-webchat/inject.js/`);
      script.setAttribute('id', 'botpress-script');
      script.addEventListener('load', injectSettings); //set up the load listener ...

      document.body.appendChild(script); //...before adding the script element to the page
      }

      function injectSettings(){
      console.log('Injecting settings');

      const settings = document.createElement('script');
      settings.setAttribute('id', 'botpress-settings');
      settings.innerHTML = `window.botpressWebChat.init({host: '${host}'})`;
      settings.addEventListener('load', firstTimeTrigger); //set up listener...

      document.body.appendChild(settings); //...before injecting code
      }

      function firstTimeTrigger(){
      console.log('First Time Trigger');
      chrome.runtime.sendMessage({type: 'isFirstTime'}, function(response) {
      if(response == true){

      const trigger = document.createElement('script');
      trigger.innerHTML = `
      window.botpressWebChat.sendEvent({ type: 'show' });
      window.botpressWebChat.sendEvent({ type: 'proactive-trigger', platform: 'web', text: ${JSON.stringify(config)} });
      `;
      document.body.appendChild(trigger);
      }
      });
      }

      injectChat();





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Welcome to Javascript's callback hell :)



        In order to run a function after the previous script has finished, you have to call it at the end of the script's load event. Remember to set up the load listener before actually adding the script element to the page. For example:



        function injectChat(){
        console.log('Injecting Chat');

        const script = document.createElement('script');
        script.setAttribute('type','text/javascript');
        script.setAttribute('src', `${host}/api/botpress-platform-webchat/inject.js/`);
        script.setAttribute('id', 'botpress-script');
        script.addEventListener('load', injectSettings); //set up the load listener ...

        document.body.appendChild(script); //...before adding the script element to the page
        }

        function injectSettings(){
        console.log('Injecting settings');

        const settings = document.createElement('script');
        settings.setAttribute('id', 'botpress-settings');
        settings.innerHTML = `window.botpressWebChat.init({host: '${host}'})`;
        settings.addEventListener('load', firstTimeTrigger); //set up listener...

        document.body.appendChild(settings); //...before injecting code
        }

        function firstTimeTrigger(){
        console.log('First Time Trigger');
        chrome.runtime.sendMessage({type: 'isFirstTime'}, function(response) {
        if(response == true){

        const trigger = document.createElement('script');
        trigger.innerHTML = `
        window.botpressWebChat.sendEvent({ type: 'show' });
        window.botpressWebChat.sendEvent({ type: 'proactive-trigger', platform: 'web', text: ${JSON.stringify(config)} });
        `;
        document.body.appendChild(trigger);
        }
        });
        }

        injectChat();





        share|improve this answer












        Welcome to Javascript's callback hell :)



        In order to run a function after the previous script has finished, you have to call it at the end of the script's load event. Remember to set up the load listener before actually adding the script element to the page. For example:



        function injectChat(){
        console.log('Injecting Chat');

        const script = document.createElement('script');
        script.setAttribute('type','text/javascript');
        script.setAttribute('src', `${host}/api/botpress-platform-webchat/inject.js/`);
        script.setAttribute('id', 'botpress-script');
        script.addEventListener('load', injectSettings); //set up the load listener ...

        document.body.appendChild(script); //...before adding the script element to the page
        }

        function injectSettings(){
        console.log('Injecting settings');

        const settings = document.createElement('script');
        settings.setAttribute('id', 'botpress-settings');
        settings.innerHTML = `window.botpressWebChat.init({host: '${host}'})`;
        settings.addEventListener('load', firstTimeTrigger); //set up listener...

        document.body.appendChild(settings); //...before injecting code
        }

        function firstTimeTrigger(){
        console.log('First Time Trigger');
        chrome.runtime.sendMessage({type: 'isFirstTime'}, function(response) {
        if(response == true){

        const trigger = document.createElement('script');
        trigger.innerHTML = `
        window.botpressWebChat.sendEvent({ type: 'show' });
        window.botpressWebChat.sendEvent({ type: 'proactive-trigger', platform: 'web', text: ${JSON.stringify(config)} });
        `;
        document.body.appendChild(trigger);
        }
        });
        }

        injectChat();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 6 at 10:49









        Iván Nokonoko

        2,5332612




        2,5332612






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148007%2fhow-to-call-one-function-after-another-in-chrome-extension-content-script%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()