I am making a userscript for facebook that would help translate text using Google Translate Api. Script is injecting html and css content successfully into facebook.
Problem is with Google Translate Api. I am injecting a script tag
var s = document.createElement('script');
s.type="text/javascript";
s.src='https://www.google.com/jsapi?key=AIzaSyD24A-czAdTj8pPc5ugo0bYiPRx8Rc2pXo';
document.body.appendChild(s);
First this script is loading the url 2 or 3 times.
To actually use the Language Api I am injecting another script tag
var ldfun = document.createElement('script');
ldfun.setAttribute('type', 'application/javascript');
ldfun.textContent= "google.load('language','1');";
document.body.appendChild(ldfun);
this script is not running and sometimes it runs then the page naviages away.
Help please
Make sure the script is not running in iFrames and then use a delay to ensure the library JS is loaded (Otherwise, the browser will run those 2
scripts asynchronously.)Something like this: