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.
javascript google-chrome-extension
add a comment |
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.
javascript google-chrome-extension
Looks like thefirstTimeTrigger()
function will trigger only once after theinjectChat()
completed. Why don't you mergefirstTimeTrigger()
at the end ofinjectChat()
?
– elegant-user
Nov 5 at 6:11
Yeah I wish. It just doesn't work
– Nilay Modi
Nov 5 at 6:27
add a comment |
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.
javascript google-chrome-extension
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
javascript google-chrome-extension
edited Nov 5 at 4:48
asked Nov 5 at 3:40
Nilay Modi
63
63
Looks like thefirstTimeTrigger()
function will trigger only once after theinjectChat()
completed. Why don't you mergefirstTimeTrigger()
at the end ofinjectChat()
?
– elegant-user
Nov 5 at 6:11
Yeah I wish. It just doesn't work
– Nilay Modi
Nov 5 at 6:27
add a comment |
Looks like thefirstTimeTrigger()
function will trigger only once after theinjectChat()
completed. Why don't you mergefirstTimeTrigger()
at the end ofinjectChat()
?
– 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
add a comment |
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();
}
That doesn't work because the function is called before the body of injectChat gets loaded.
– Nilay Modi
Nov 5 at 4:46
add a comment |
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();
add a comment |
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();
}
That doesn't work because the function is called before the body of injectChat gets loaded.
– Nilay Modi
Nov 5 at 4:46
add a comment |
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();
}
That doesn't work because the function is called before the body of injectChat gets loaded.
– Nilay Modi
Nov 5 at 4:46
add a comment |
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();
}
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();
}
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
add a comment |
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
add a comment |
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();
add a comment |
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();
add a comment |
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();
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();
answered Nov 6 at 10:49
Iván Nokonoko
2,5332612
2,5332612
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Looks like the
firstTimeTrigger()
function will trigger only once after theinjectChat()
completed. Why don't you mergefirstTimeTrigger()
at the end ofinjectChat()
?– elegant-user
Nov 5 at 6:11
Yeah I wish. It just doesn't work
– Nilay Modi
Nov 5 at 6:27