Chrome Extension: How to convert a local HTML file into a URL to display?












1















I am currently developing a chrome extension, and am attempting to make one of my local HTML files into a URL so I can open it from my content script. One solution I found was to use:



chrome.tabs.create({url: chrome.extension.getURL('notes.html')});


This did not work though. Some people have reported that content scripts may not work with all of the chrome extension API. I need this function to work from my content script though, to make sure it will fire when I need it to. I also found:



var urlChanged = window.url.createObjectURL("notes.html");
window.open(urlChanged);


This did not work either. I ended with trying:



var urlChanged = chrome.runtime.getURL("notes.html");
window.open(urlChanged);


A new tab opens, but I only get a blank HTML page. I was wondering if anyone can give me some insight as to why none of these methods may be working?



tl;dr My content script does not want to open and display a local HTML file I made. I used multiple methods to try and open it, but the file does not want to open from the content script. The HTML file is in the same extension folder as the content script and manifest.json. Any help with this would be appreciated!










share|improve this question



























    1















    I am currently developing a chrome extension, and am attempting to make one of my local HTML files into a URL so I can open it from my content script. One solution I found was to use:



    chrome.tabs.create({url: chrome.extension.getURL('notes.html')});


    This did not work though. Some people have reported that content scripts may not work with all of the chrome extension API. I need this function to work from my content script though, to make sure it will fire when I need it to. I also found:



    var urlChanged = window.url.createObjectURL("notes.html");
    window.open(urlChanged);


    This did not work either. I ended with trying:



    var urlChanged = chrome.runtime.getURL("notes.html");
    window.open(urlChanged);


    A new tab opens, but I only get a blank HTML page. I was wondering if anyone can give me some insight as to why none of these methods may be working?



    tl;dr My content script does not want to open and display a local HTML file I made. I used multiple methods to try and open it, but the file does not want to open from the content script. The HTML file is in the same extension folder as the content script and manifest.json. Any help with this would be appreciated!










    share|improve this question

























      1












      1








      1








      I am currently developing a chrome extension, and am attempting to make one of my local HTML files into a URL so I can open it from my content script. One solution I found was to use:



      chrome.tabs.create({url: chrome.extension.getURL('notes.html')});


      This did not work though. Some people have reported that content scripts may not work with all of the chrome extension API. I need this function to work from my content script though, to make sure it will fire when I need it to. I also found:



      var urlChanged = window.url.createObjectURL("notes.html");
      window.open(urlChanged);


      This did not work either. I ended with trying:



      var urlChanged = chrome.runtime.getURL("notes.html");
      window.open(urlChanged);


      A new tab opens, but I only get a blank HTML page. I was wondering if anyone can give me some insight as to why none of these methods may be working?



      tl;dr My content script does not want to open and display a local HTML file I made. I used multiple methods to try and open it, but the file does not want to open from the content script. The HTML file is in the same extension folder as the content script and manifest.json. Any help with this would be appreciated!










      share|improve this question














      I am currently developing a chrome extension, and am attempting to make one of my local HTML files into a URL so I can open it from my content script. One solution I found was to use:



      chrome.tabs.create({url: chrome.extension.getURL('notes.html')});


      This did not work though. Some people have reported that content scripts may not work with all of the chrome extension API. I need this function to work from my content script though, to make sure it will fire when I need it to. I also found:



      var urlChanged = window.url.createObjectURL("notes.html");
      window.open(urlChanged);


      This did not work either. I ended with trying:



      var urlChanged = chrome.runtime.getURL("notes.html");
      window.open(urlChanged);


      A new tab opens, but I only get a blank HTML page. I was wondering if anyone can give me some insight as to why none of these methods may be working?



      tl;dr My content script does not want to open and display a local HTML file I made. I used multiple methods to try and open it, but the file does not want to open from the content script. The HTML file is in the same extension folder as the content script and manifest.json. Any help with this would be appreciated!







      javascript url google-chrome-extension






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 18:12









      Augusto CelisAugusto Celis

      84




      84
























          1 Answer
          1






          active

          oldest

          votes


















          1
















          • To open your extension's page via window.open() in a content script you'll have to expose it using web_accessible_resources in manifest.json. It'll make your extension detectable from web though.



            "web_accessible_resources": [
            "notes.html"
            ]






          • A better solution is to send a message from the content script to the background script, which can use chrome.tabs API to open your extension page.



            It's a better solution because window.open from a web page tab may be blocked by the anti-popup policy. And also because you don't expose your extension's pages to the web.



            manifest.json should declare the background script:



            "background": {
            "scripts": ["background.js"],
            "persistent": false
            }


            content script simply sends a message:



            chrome.runtime.sendMessage({action: 'openNotes'});


            background.js does the work:



            chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
            switch (msg.action) {
            case 'openNotes':
            chrome.tabs.create({
            url: '/notes.html',
            active: true,
            index: sender.tab.index + 1,
            openerTabId: sender.tab.id,
            });
            return;
            }
            });







          share|improve this answer























            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%2f53418218%2fchrome-extension-how-to-convert-a-local-html-file-into-a-url-to-display%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
















            • To open your extension's page via window.open() in a content script you'll have to expose it using web_accessible_resources in manifest.json. It'll make your extension detectable from web though.



              "web_accessible_resources": [
              "notes.html"
              ]






            • A better solution is to send a message from the content script to the background script, which can use chrome.tabs API to open your extension page.



              It's a better solution because window.open from a web page tab may be blocked by the anti-popup policy. And also because you don't expose your extension's pages to the web.



              manifest.json should declare the background script:



              "background": {
              "scripts": ["background.js"],
              "persistent": false
              }


              content script simply sends a message:



              chrome.runtime.sendMessage({action: 'openNotes'});


              background.js does the work:



              chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
              switch (msg.action) {
              case 'openNotes':
              chrome.tabs.create({
              url: '/notes.html',
              active: true,
              index: sender.tab.index + 1,
              openerTabId: sender.tab.id,
              });
              return;
              }
              });







            share|improve this answer




























              1
















              • To open your extension's page via window.open() in a content script you'll have to expose it using web_accessible_resources in manifest.json. It'll make your extension detectable from web though.



                "web_accessible_resources": [
                "notes.html"
                ]






              • A better solution is to send a message from the content script to the background script, which can use chrome.tabs API to open your extension page.



                It's a better solution because window.open from a web page tab may be blocked by the anti-popup policy. And also because you don't expose your extension's pages to the web.



                manifest.json should declare the background script:



                "background": {
                "scripts": ["background.js"],
                "persistent": false
                }


                content script simply sends a message:



                chrome.runtime.sendMessage({action: 'openNotes'});


                background.js does the work:



                chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
                switch (msg.action) {
                case 'openNotes':
                chrome.tabs.create({
                url: '/notes.html',
                active: true,
                index: sender.tab.index + 1,
                openerTabId: sender.tab.id,
                });
                return;
                }
                });







              share|improve this answer


























                1












                1








                1









                • To open your extension's page via window.open() in a content script you'll have to expose it using web_accessible_resources in manifest.json. It'll make your extension detectable from web though.



                  "web_accessible_resources": [
                  "notes.html"
                  ]






                • A better solution is to send a message from the content script to the background script, which can use chrome.tabs API to open your extension page.



                  It's a better solution because window.open from a web page tab may be blocked by the anti-popup policy. And also because you don't expose your extension's pages to the web.



                  manifest.json should declare the background script:



                  "background": {
                  "scripts": ["background.js"],
                  "persistent": false
                  }


                  content script simply sends a message:



                  chrome.runtime.sendMessage({action: 'openNotes'});


                  background.js does the work:



                  chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
                  switch (msg.action) {
                  case 'openNotes':
                  chrome.tabs.create({
                  url: '/notes.html',
                  active: true,
                  index: sender.tab.index + 1,
                  openerTabId: sender.tab.id,
                  });
                  return;
                  }
                  });







                share|improve this answer















                • To open your extension's page via window.open() in a content script you'll have to expose it using web_accessible_resources in manifest.json. It'll make your extension detectable from web though.



                  "web_accessible_resources": [
                  "notes.html"
                  ]






                • A better solution is to send a message from the content script to the background script, which can use chrome.tabs API to open your extension page.



                  It's a better solution because window.open from a web page tab may be blocked by the anti-popup policy. And also because you don't expose your extension's pages to the web.



                  manifest.json should declare the background script:



                  "background": {
                  "scripts": ["background.js"],
                  "persistent": false
                  }


                  content script simply sends a message:



                  chrome.runtime.sendMessage({action: 'openNotes'});


                  background.js does the work:



                  chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
                  switch (msg.action) {
                  case 'openNotes':
                  chrome.tabs.create({
                  url: '/notes.html',
                  active: true,
                  index: sender.tab.index + 1,
                  openerTabId: sender.tab.id,
                  });
                  return;
                  }
                  });








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 18:38









                wOxxOmwOxxOm

                27.6k44862




                27.6k44862
































                    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%2f53418218%2fchrome-extension-how-to-convert-a-local-html-file-into-a-url-to-display%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

                    Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                    ComboBox Display Member on multiple fields

                    Is it possible to collect Nectar points via Trainline?