Setting a placeholder image/HTML while electron can't connect to URL












3














I'm trying to display an image in my electron app that would indicate that the app couldn't load the URL. Also, the app should keep trying to connect again to the server every 3 seconds. Basically, the behavior I'm looking for is like in chrome when you get this screen:



So far, I've managed to get the part where the app persistently keeps trying to connect, but I can't figure out how to display custom HTML in case the connection fails. This is what I have so far:



const electron = require('electron');


function connect(win, host, port) {
win.loadURL(`http://${host}:${port}`);
}


function main() {
const win = new electron.BrowserWindow({
icon: __dirname + '/favicon.ico',
show: false
});
const host = process.argv.includes('--host') ? process.argv[process.argv.findIndex(arg => arg == '--host') + 1] : 'localhost';
const port = process.argv.includes('--port') ? process.argv[process.argv.findIndex(arg => arg == '--port') + 1] : '12345';
global.argv = process.argv;
global.env = process.env;
connect(win, host, port);

win.webContents.on('did-fail-load', () => {
setTimeout(() => connect(win, host, port), 3000);
});

win.once('ready-to-show', () => {
win.show();
win.focus();
});
}


electron.app.on('ready', main);


I've tried passing custom HTML via html data URL (using the loadURL) function, but every time I use loadURL again when reconnecting to the server, the "custom" HTML gets overwritten by a white screen.










share|improve this question





























    3














    I'm trying to display an image in my electron app that would indicate that the app couldn't load the URL. Also, the app should keep trying to connect again to the server every 3 seconds. Basically, the behavior I'm looking for is like in chrome when you get this screen:



    So far, I've managed to get the part where the app persistently keeps trying to connect, but I can't figure out how to display custom HTML in case the connection fails. This is what I have so far:



    const electron = require('electron');


    function connect(win, host, port) {
    win.loadURL(`http://${host}:${port}`);
    }


    function main() {
    const win = new electron.BrowserWindow({
    icon: __dirname + '/favicon.ico',
    show: false
    });
    const host = process.argv.includes('--host') ? process.argv[process.argv.findIndex(arg => arg == '--host') + 1] : 'localhost';
    const port = process.argv.includes('--port') ? process.argv[process.argv.findIndex(arg => arg == '--port') + 1] : '12345';
    global.argv = process.argv;
    global.env = process.env;
    connect(win, host, port);

    win.webContents.on('did-fail-load', () => {
    setTimeout(() => connect(win, host, port), 3000);
    });

    win.once('ready-to-show', () => {
    win.show();
    win.focus();
    });
    }


    electron.app.on('ready', main);


    I've tried passing custom HTML via html data URL (using the loadURL) function, but every time I use loadURL again when reconnecting to the server, the "custom" HTML gets overwritten by a white screen.










    share|improve this question



























      3












      3








      3







      I'm trying to display an image in my electron app that would indicate that the app couldn't load the URL. Also, the app should keep trying to connect again to the server every 3 seconds. Basically, the behavior I'm looking for is like in chrome when you get this screen:



      So far, I've managed to get the part where the app persistently keeps trying to connect, but I can't figure out how to display custom HTML in case the connection fails. This is what I have so far:



      const electron = require('electron');


      function connect(win, host, port) {
      win.loadURL(`http://${host}:${port}`);
      }


      function main() {
      const win = new electron.BrowserWindow({
      icon: __dirname + '/favicon.ico',
      show: false
      });
      const host = process.argv.includes('--host') ? process.argv[process.argv.findIndex(arg => arg == '--host') + 1] : 'localhost';
      const port = process.argv.includes('--port') ? process.argv[process.argv.findIndex(arg => arg == '--port') + 1] : '12345';
      global.argv = process.argv;
      global.env = process.env;
      connect(win, host, port);

      win.webContents.on('did-fail-load', () => {
      setTimeout(() => connect(win, host, port), 3000);
      });

      win.once('ready-to-show', () => {
      win.show();
      win.focus();
      });
      }


      electron.app.on('ready', main);


      I've tried passing custom HTML via html data URL (using the loadURL) function, but every time I use loadURL again when reconnecting to the server, the "custom" HTML gets overwritten by a white screen.










      share|improve this question















      I'm trying to display an image in my electron app that would indicate that the app couldn't load the URL. Also, the app should keep trying to connect again to the server every 3 seconds. Basically, the behavior I'm looking for is like in chrome when you get this screen:



      So far, I've managed to get the part where the app persistently keeps trying to connect, but I can't figure out how to display custom HTML in case the connection fails. This is what I have so far:



      const electron = require('electron');


      function connect(win, host, port) {
      win.loadURL(`http://${host}:${port}`);
      }


      function main() {
      const win = new electron.BrowserWindow({
      icon: __dirname + '/favicon.ico',
      show: false
      });
      const host = process.argv.includes('--host') ? process.argv[process.argv.findIndex(arg => arg == '--host') + 1] : 'localhost';
      const port = process.argv.includes('--port') ? process.argv[process.argv.findIndex(arg => arg == '--port') + 1] : '12345';
      global.argv = process.argv;
      global.env = process.env;
      connect(win, host, port);

      win.webContents.on('did-fail-load', () => {
      setTimeout(() => connect(win, host, port), 3000);
      });

      win.once('ready-to-show', () => {
      win.show();
      win.focus();
      });
      }


      electron.app.on('ready', main);


      I've tried passing custom HTML via html data URL (using the loadURL) function, but every time I use loadURL again when reconnecting to the server, the "custom" HTML gets overwritten by a white screen.







      javascript electron






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 9:28

























      asked Nov 20 '18 at 8:21









      Zlatan Sičanica

      507




      507
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You're basically there. All that's left is loading your error page.



          Say you have an error.html:



          <body>
          <h1>Error connecting</h1>
          </body>


          Your main code could look like this:



          let mainWindow
          function createWindow () {
          mainWindow = new BrowserWindow({width: 800, height: 600})

          mainWindow.webContents.on("did-fail-load", () => {
          mainWindow.loadFile("error.html");
          setTimeout(connect, 3000);
          });

          connect();
          }

          function connect() {
          console.log("trying to connect");
          mainWindow.loadURL("https://madeupsitexxxxxxxxxxx.com");
          }

          app.on('ready', createWindow);





          share|improve this answer























          • Yup, that was it, thanks for the help!
            – Zlatan Sičanica
            Nov 21 '18 at 7:47











          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',
          autoActivateHeartbeat: false,
          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%2f53388829%2fsetting-a-placeholder-image-html-while-electron-cant-connect-to-url%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You're basically there. All that's left is loading your error page.



          Say you have an error.html:



          <body>
          <h1>Error connecting</h1>
          </body>


          Your main code could look like this:



          let mainWindow
          function createWindow () {
          mainWindow = new BrowserWindow({width: 800, height: 600})

          mainWindow.webContents.on("did-fail-load", () => {
          mainWindow.loadFile("error.html");
          setTimeout(connect, 3000);
          });

          connect();
          }

          function connect() {
          console.log("trying to connect");
          mainWindow.loadURL("https://madeupsitexxxxxxxxxxx.com");
          }

          app.on('ready', createWindow);





          share|improve this answer























          • Yup, that was it, thanks for the help!
            – Zlatan Sičanica
            Nov 21 '18 at 7:47
















          1














          You're basically there. All that's left is loading your error page.



          Say you have an error.html:



          <body>
          <h1>Error connecting</h1>
          </body>


          Your main code could look like this:



          let mainWindow
          function createWindow () {
          mainWindow = new BrowserWindow({width: 800, height: 600})

          mainWindow.webContents.on("did-fail-load", () => {
          mainWindow.loadFile("error.html");
          setTimeout(connect, 3000);
          });

          connect();
          }

          function connect() {
          console.log("trying to connect");
          mainWindow.loadURL("https://madeupsitexxxxxxxxxxx.com");
          }

          app.on('ready', createWindow);





          share|improve this answer























          • Yup, that was it, thanks for the help!
            – Zlatan Sičanica
            Nov 21 '18 at 7:47














          1












          1








          1






          You're basically there. All that's left is loading your error page.



          Say you have an error.html:



          <body>
          <h1>Error connecting</h1>
          </body>


          Your main code could look like this:



          let mainWindow
          function createWindow () {
          mainWindow = new BrowserWindow({width: 800, height: 600})

          mainWindow.webContents.on("did-fail-load", () => {
          mainWindow.loadFile("error.html");
          setTimeout(connect, 3000);
          });

          connect();
          }

          function connect() {
          console.log("trying to connect");
          mainWindow.loadURL("https://madeupsitexxxxxxxxxxx.com");
          }

          app.on('ready', createWindow);





          share|improve this answer














          You're basically there. All that's left is loading your error page.



          Say you have an error.html:



          <body>
          <h1>Error connecting</h1>
          </body>


          Your main code could look like this:



          let mainWindow
          function createWindow () {
          mainWindow = new BrowserWindow({width: 800, height: 600})

          mainWindow.webContents.on("did-fail-load", () => {
          mainWindow.loadFile("error.html");
          setTimeout(connect, 3000);
          });

          connect();
          }

          function connect() {
          console.log("trying to connect");
          mainWindow.loadURL("https://madeupsitexxxxxxxxxxx.com");
          }

          app.on('ready', createWindow);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 20:01

























          answered Nov 20 '18 at 19:56









          pushkin

          3,937112651




          3,937112651












          • Yup, that was it, thanks for the help!
            – Zlatan Sičanica
            Nov 21 '18 at 7:47


















          • Yup, that was it, thanks for the help!
            – Zlatan Sičanica
            Nov 21 '18 at 7:47
















          Yup, that was it, thanks for the help!
          – Zlatan Sičanica
          Nov 21 '18 at 7:47




          Yup, that was it, thanks for the help!
          – Zlatan Sičanica
          Nov 21 '18 at 7:47


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388829%2fsetting-a-placeholder-image-html-while-electron-cant-connect-to-url%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]