Q:

Electron-Insert HTML content into webview while loading

I want to insert few html tags into electron webview while loading.
I tried below code in preload.js but that doesn't have any effect.
preload.js
window.onload = function() {
    var script = document.createElement("script");
    script.src = "https://code.jquery.com/jquery-2.1.4.min.js";
    script.onload = script.onreadystatechange = function() {
      $(document).ready(function() {

      document.getElementsByTagName("head")[0].innerHTML += "<div id="google_translate_element"></div><script type="text/javascript">function googleTranslateElementInit() {new google.translate.TranslateElement({pageLanguage: 'de', includedLanguages: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');}</script><script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>";

};

Index.html
<!DOCTYPE html>
<html>
  <head>
    <title>jQuery injection into webview preload</title>
  </head>
  <body style="overflow:hidden;">
    <webview id="webview" preload="./preload.js" src="https://www.amazon.de/Fortan-Business-Edelstahlarmband-Machinery-Quartz/dp/B01FZEDQZG/ref=sr_1_1?ie=UTF8&qid=1476905705&sr=8-1&keywords=watches" style="position:absolute;width:100%;height:100%;"></webview>

  </body>
</html>

Expected Result :
Page has to loaded with translate tag on top
Current result :
No translate tag is appearing

A:

window.onload = function() {

var script = document.createElement("script");
script.src = "https://code.jquery.com/jquery-2.1.4.min.js";
script.onload = script.onreadystatechange = function() {
  $(document).ready(function() {            
        var temp = $('<div id="google_translate_element">Translate</div><script type="text/javascript">function googleTranslateElementInit() {new google.translate.TranslateElement({pageLanguage: \'de\', includedLanguages: \'en\', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, \'google_translate_element\');}</script><script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>');         
        $( "<div id='translateCode'></div>" ).prependTo( 'body' );
        $("#translateCode").append(temp);
  });
};
document.body.appendChild(script);
};

It works. Tested.

