Displaying post response (XMLHttpRequest) in chrome extension pop-up apart from python console












0















I am creating chrome extension that is sending post request with information about specific url that is currently browsed by user to flask (local host) and then some web scraping on this url is done and based on the obtained information a category is assigned. After this process is done I would like the popup to show assigned category when user is on page(s) declared earlier (ofc without user action e.g. clicking on extension icon). At the moment I see this happening on python console but I'm not able to see this on popup which appears blank (I presume that this is because the script needs about 1-3 seconds to perform and listener or some kind of timeout is needed to reach my goal, however I'm total JS newbie at the moment and I cant find right solution). When I click 'ok' on the popup then I see that on the background.js in dev tools there is information that I am looking for. However I need to have this on the popup. How can I do this? I described my goal and actions that are taken on the image below -
https://imgur.com/a/8Jknf1r



Code that I use (background.js) with stackoverflow as exemplary page looks like this:



function onCommit(info) {
const xhr = new XMLHttpRequest();
xhr.onload = () => {
console.log('onload %d:%d', info.tabId, info.frameId, info.url);
};
xhr.open('POST', 'http://localhost:5000/',true);
xhr.send(info.url);
// xhr.addEventListener("readystatechange", function () {
console.log(xhr.responseText);
alert(xhr.responseText);
}};

chrome.webNavigation.onCommitted.addListener(onCommit, {
url: [
{hostEquals: 'www.stackoverflow.com'},
{urlPrefix: 'https://stackoverflow.com/'},
{urlMatches: '^https://(www\.)?stackoverflow.com/.*$'},
],
});


alert(xhr.responseText) at the moment results in blank popup - what to do to fill it with response?










share|improve this question





























    0















    I am creating chrome extension that is sending post request with information about specific url that is currently browsed by user to flask (local host) and then some web scraping on this url is done and based on the obtained information a category is assigned. After this process is done I would like the popup to show assigned category when user is on page(s) declared earlier (ofc without user action e.g. clicking on extension icon). At the moment I see this happening on python console but I'm not able to see this on popup which appears blank (I presume that this is because the script needs about 1-3 seconds to perform and listener or some kind of timeout is needed to reach my goal, however I'm total JS newbie at the moment and I cant find right solution). When I click 'ok' on the popup then I see that on the background.js in dev tools there is information that I am looking for. However I need to have this on the popup. How can I do this? I described my goal and actions that are taken on the image below -
    https://imgur.com/a/8Jknf1r



    Code that I use (background.js) with stackoverflow as exemplary page looks like this:



    function onCommit(info) {
    const xhr = new XMLHttpRequest();
    xhr.onload = () => {
    console.log('onload %d:%d', info.tabId, info.frameId, info.url);
    };
    xhr.open('POST', 'http://localhost:5000/',true);
    xhr.send(info.url);
    // xhr.addEventListener("readystatechange", function () {
    console.log(xhr.responseText);
    alert(xhr.responseText);
    }};

    chrome.webNavigation.onCommitted.addListener(onCommit, {
    url: [
    {hostEquals: 'www.stackoverflow.com'},
    {urlPrefix: 'https://stackoverflow.com/'},
    {urlMatches: '^https://(www\.)?stackoverflow.com/.*$'},
    ],
    });


    alert(xhr.responseText) at the moment results in blank popup - what to do to fill it with response?










    share|improve this question



























      0












      0








      0








      I am creating chrome extension that is sending post request with information about specific url that is currently browsed by user to flask (local host) and then some web scraping on this url is done and based on the obtained information a category is assigned. After this process is done I would like the popup to show assigned category when user is on page(s) declared earlier (ofc without user action e.g. clicking on extension icon). At the moment I see this happening on python console but I'm not able to see this on popup which appears blank (I presume that this is because the script needs about 1-3 seconds to perform and listener or some kind of timeout is needed to reach my goal, however I'm total JS newbie at the moment and I cant find right solution). When I click 'ok' on the popup then I see that on the background.js in dev tools there is information that I am looking for. However I need to have this on the popup. How can I do this? I described my goal and actions that are taken on the image below -
      https://imgur.com/a/8Jknf1r



      Code that I use (background.js) with stackoverflow as exemplary page looks like this:



      function onCommit(info) {
      const xhr = new XMLHttpRequest();
      xhr.onload = () => {
      console.log('onload %d:%d', info.tabId, info.frameId, info.url);
      };
      xhr.open('POST', 'http://localhost:5000/',true);
      xhr.send(info.url);
      // xhr.addEventListener("readystatechange", function () {
      console.log(xhr.responseText);
      alert(xhr.responseText);
      }};

      chrome.webNavigation.onCommitted.addListener(onCommit, {
      url: [
      {hostEquals: 'www.stackoverflow.com'},
      {urlPrefix: 'https://stackoverflow.com/'},
      {urlMatches: '^https://(www\.)?stackoverflow.com/.*$'},
      ],
      });


      alert(xhr.responseText) at the moment results in blank popup - what to do to fill it with response?










      share|improve this question
















      I am creating chrome extension that is sending post request with information about specific url that is currently browsed by user to flask (local host) and then some web scraping on this url is done and based on the obtained information a category is assigned. After this process is done I would like the popup to show assigned category when user is on page(s) declared earlier (ofc without user action e.g. clicking on extension icon). At the moment I see this happening on python console but I'm not able to see this on popup which appears blank (I presume that this is because the script needs about 1-3 seconds to perform and listener or some kind of timeout is needed to reach my goal, however I'm total JS newbie at the moment and I cant find right solution). When I click 'ok' on the popup then I see that on the background.js in dev tools there is information that I am looking for. However I need to have this on the popup. How can I do this? I described my goal and actions that are taken on the image below -
      https://imgur.com/a/8Jknf1r



      Code that I use (background.js) with stackoverflow as exemplary page looks like this:



      function onCommit(info) {
      const xhr = new XMLHttpRequest();
      xhr.onload = () => {
      console.log('onload %d:%d', info.tabId, info.frameId, info.url);
      };
      xhr.open('POST', 'http://localhost:5000/',true);
      xhr.send(info.url);
      // xhr.addEventListener("readystatechange", function () {
      console.log(xhr.responseText);
      alert(xhr.responseText);
      }};

      chrome.webNavigation.onCommitted.addListener(onCommit, {
      url: [
      {hostEquals: 'www.stackoverflow.com'},
      {urlPrefix: 'https://stackoverflow.com/'},
      {urlMatches: '^https://(www\.)?stackoverflow.com/.*$'},
      ],
      });


      alert(xhr.responseText) at the moment results in blank popup - what to do to fill it with response?







      javascript ajax google-chrome-extension






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 13:58









      davidism

      65k12172188




      65k12172188










      asked Jan 2 at 11:23









      silva1818silva1818

      34




      34
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Ok I figured it out, just typical newbie error concerning syntax near listener; code that works:



          function onCommit(info) {
          const xhr = new XMLHttpRequest();
          xhr.onload = () => {
          console.log('onload %d:%d', info.tabId, info.frameId, info.url);
          };
          xhr.open('POST', 'http://localhost:5000/',true);
          xhr.send(info.url);
          xhr.addEventListener("readystatechange", function () {
          console.log(xhr.responseText);
          alert(xhr.responseText);
          })
          };
          chrome.webNavigation.onCommitted.addListener(onCommit, {
          url: [
          {hostEquals: 'www.stackoverflow.com'},
          {urlPrefix: 'https://stackoverflow.com/'},
          {urlMatches: '^https://(www\.)?stackoverflow.com/.*$'},
          ],
          });


          One issue that is strange for me is that the popup now appears 3 times, first blank, then click ok, I've got an answer (which was expected) and then after clicking again it appears again - is it specific or may I change that?






          share|improve this answer
























          • readystatechange is raised multiple times through the request's lifetime, not just on completion. That explains the behavior you're seeing. Either use onload (you're already doing it after all) or check the actual state in the listener before doing anything else.

            – Xan
            Jan 3 at 12:28











          • thanks for a hint; i have another question - would it be possible to have a mutable host? for example user via excel/txt (whatever) defines url that needs to be listened;basically this info would be stored in db - would it be possible to transfer it to background.js to these fields: hostEquals,urlPrefix,urlMatches? what is the easiest way to resolve this? Thanks in advance

            – silva1818
            Jan 14 at 7:46











          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%2f54005443%2fdisplaying-post-response-xmlhttprequest-in-chrome-extension-pop-up-apart-from%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









          0














          Ok I figured it out, just typical newbie error concerning syntax near listener; code that works:



          function onCommit(info) {
          const xhr = new XMLHttpRequest();
          xhr.onload = () => {
          console.log('onload %d:%d', info.tabId, info.frameId, info.url);
          };
          xhr.open('POST', 'http://localhost:5000/',true);
          xhr.send(info.url);
          xhr.addEventListener("readystatechange", function () {
          console.log(xhr.responseText);
          alert(xhr.responseText);
          })
          };
          chrome.webNavigation.onCommitted.addListener(onCommit, {
          url: [
          {hostEquals: 'www.stackoverflow.com'},
          {urlPrefix: 'https://stackoverflow.com/'},
          {urlMatches: '^https://(www\.)?stackoverflow.com/.*$'},
          ],
          });


          One issue that is strange for me is that the popup now appears 3 times, first blank, then click ok, I've got an answer (which was expected) and then after clicking again it appears again - is it specific or may I change that?






          share|improve this answer
























          • readystatechange is raised multiple times through the request's lifetime, not just on completion. That explains the behavior you're seeing. Either use onload (you're already doing it after all) or check the actual state in the listener before doing anything else.

            – Xan
            Jan 3 at 12:28











          • thanks for a hint; i have another question - would it be possible to have a mutable host? for example user via excel/txt (whatever) defines url that needs to be listened;basically this info would be stored in db - would it be possible to transfer it to background.js to these fields: hostEquals,urlPrefix,urlMatches? what is the easiest way to resolve this? Thanks in advance

            – silva1818
            Jan 14 at 7:46
















          0














          Ok I figured it out, just typical newbie error concerning syntax near listener; code that works:



          function onCommit(info) {
          const xhr = new XMLHttpRequest();
          xhr.onload = () => {
          console.log('onload %d:%d', info.tabId, info.frameId, info.url);
          };
          xhr.open('POST', 'http://localhost:5000/',true);
          xhr.send(info.url);
          xhr.addEventListener("readystatechange", function () {
          console.log(xhr.responseText);
          alert(xhr.responseText);
          })
          };
          chrome.webNavigation.onCommitted.addListener(onCommit, {
          url: [
          {hostEquals: 'www.stackoverflow.com'},
          {urlPrefix: 'https://stackoverflow.com/'},
          {urlMatches: '^https://(www\.)?stackoverflow.com/.*$'},
          ],
          });


          One issue that is strange for me is that the popup now appears 3 times, first blank, then click ok, I've got an answer (which was expected) and then after clicking again it appears again - is it specific or may I change that?






          share|improve this answer
























          • readystatechange is raised multiple times through the request's lifetime, not just on completion. That explains the behavior you're seeing. Either use onload (you're already doing it after all) or check the actual state in the listener before doing anything else.

            – Xan
            Jan 3 at 12:28











          • thanks for a hint; i have another question - would it be possible to have a mutable host? for example user via excel/txt (whatever) defines url that needs to be listened;basically this info would be stored in db - would it be possible to transfer it to background.js to these fields: hostEquals,urlPrefix,urlMatches? what is the easiest way to resolve this? Thanks in advance

            – silva1818
            Jan 14 at 7:46














          0












          0








          0







          Ok I figured it out, just typical newbie error concerning syntax near listener; code that works:



          function onCommit(info) {
          const xhr = new XMLHttpRequest();
          xhr.onload = () => {
          console.log('onload %d:%d', info.tabId, info.frameId, info.url);
          };
          xhr.open('POST', 'http://localhost:5000/',true);
          xhr.send(info.url);
          xhr.addEventListener("readystatechange", function () {
          console.log(xhr.responseText);
          alert(xhr.responseText);
          })
          };
          chrome.webNavigation.onCommitted.addListener(onCommit, {
          url: [
          {hostEquals: 'www.stackoverflow.com'},
          {urlPrefix: 'https://stackoverflow.com/'},
          {urlMatches: '^https://(www\.)?stackoverflow.com/.*$'},
          ],
          });


          One issue that is strange for me is that the popup now appears 3 times, first blank, then click ok, I've got an answer (which was expected) and then after clicking again it appears again - is it specific or may I change that?






          share|improve this answer













          Ok I figured it out, just typical newbie error concerning syntax near listener; code that works:



          function onCommit(info) {
          const xhr = new XMLHttpRequest();
          xhr.onload = () => {
          console.log('onload %d:%d', info.tabId, info.frameId, info.url);
          };
          xhr.open('POST', 'http://localhost:5000/',true);
          xhr.send(info.url);
          xhr.addEventListener("readystatechange", function () {
          console.log(xhr.responseText);
          alert(xhr.responseText);
          })
          };
          chrome.webNavigation.onCommitted.addListener(onCommit, {
          url: [
          {hostEquals: 'www.stackoverflow.com'},
          {urlPrefix: 'https://stackoverflow.com/'},
          {urlMatches: '^https://(www\.)?stackoverflow.com/.*$'},
          ],
          });


          One issue that is strange for me is that the popup now appears 3 times, first blank, then click ok, I've got an answer (which was expected) and then after clicking again it appears again - is it specific or may I change that?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 7:52









          silva1818silva1818

          34




          34













          • readystatechange is raised multiple times through the request's lifetime, not just on completion. That explains the behavior you're seeing. Either use onload (you're already doing it after all) or check the actual state in the listener before doing anything else.

            – Xan
            Jan 3 at 12:28











          • thanks for a hint; i have another question - would it be possible to have a mutable host? for example user via excel/txt (whatever) defines url that needs to be listened;basically this info would be stored in db - would it be possible to transfer it to background.js to these fields: hostEquals,urlPrefix,urlMatches? what is the easiest way to resolve this? Thanks in advance

            – silva1818
            Jan 14 at 7:46



















          • readystatechange is raised multiple times through the request's lifetime, not just on completion. That explains the behavior you're seeing. Either use onload (you're already doing it after all) or check the actual state in the listener before doing anything else.

            – Xan
            Jan 3 at 12:28











          • thanks for a hint; i have another question - would it be possible to have a mutable host? for example user via excel/txt (whatever) defines url that needs to be listened;basically this info would be stored in db - would it be possible to transfer it to background.js to these fields: hostEquals,urlPrefix,urlMatches? what is the easiest way to resolve this? Thanks in advance

            – silva1818
            Jan 14 at 7:46

















          readystatechange is raised multiple times through the request's lifetime, not just on completion. That explains the behavior you're seeing. Either use onload (you're already doing it after all) or check the actual state in the listener before doing anything else.

          – Xan
          Jan 3 at 12:28





          readystatechange is raised multiple times through the request's lifetime, not just on completion. That explains the behavior you're seeing. Either use onload (you're already doing it after all) or check the actual state in the listener before doing anything else.

          – Xan
          Jan 3 at 12:28













          thanks for a hint; i have another question - would it be possible to have a mutable host? for example user via excel/txt (whatever) defines url that needs to be listened;basically this info would be stored in db - would it be possible to transfer it to background.js to these fields: hostEquals,urlPrefix,urlMatches? what is the easiest way to resolve this? Thanks in advance

          – silva1818
          Jan 14 at 7:46





          thanks for a hint; i have another question - would it be possible to have a mutable host? for example user via excel/txt (whatever) defines url that needs to be listened;basically this info would be stored in db - would it be possible to transfer it to background.js to these fields: hostEquals,urlPrefix,urlMatches? what is the easiest way to resolve this? Thanks in advance

          – silva1818
          Jan 14 at 7:46




















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005443%2fdisplaying-post-response-xmlhttprequest-in-chrome-extension-pop-up-apart-from%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

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'