0

I am trying to create a Chrome extension and for that particular reason I want my extension to read a file from my system but on clicking the file the default system folder popup opens and when I click on that the extension gets closed. That should read the data.

My code goes here:

html:

    <div class="outer_area">
    <form id="your_form" method="post" enctype="multipart/form-data">
    <input class="input-btn start-button div_one" type="file" id="fileUpload" />
    <button class="input-btn end-button div_two">Convert to sql</button>   
   </div>

popup.js

document.querySelector(".start-button").addEventListener("click", function () {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.sendMessage(tabs[0].id, {action: "start"});
    });
});
document.querySelector(".end-button").addEventListener("click", function () {
    alert('request sent');
   chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.sendMessage(tabs[0].id, {action: "end"});
    });
});

content.js:-

chrome.runtime.onMessage.addListener(   
function(request, sender, sendResponse)      
{  
  if(request.action == 'start')
  {
     alert('resquest recived');
     window.open(chrome.extension.getURL("html/popup.html"));

  }

   else if(request.action == 'end')           
   {
            alert('request recived')

    }
   else
   {
    alert('Wrong button placed')
   }


});
halfer
  • 19,824
  • 17
  • 99
  • 186
Rahul shukla
  • 378
  • 1
  • 12
  • That's how it works. When you open a new tab in the active window, the popup is automatically closed. You'll need to either open a new window using chrome.windows.create in popup.js, or open a new tab without focusing it, or switch from the default browser-action popup to a normal separate window with type: 'popup'. – wOxxOm May 04 '18 at 05:55
  • Can you tell mee how can i do that. I am new to chrome extension and i dont know how to upload a file in chrome extension – Rahul shukla May 04 '18 at 06:22
  • Oh, that's a different problem. Don't use `form` element in the popup. – wOxxOm May 04 '18 at 06:44
  • Can you give mee a breif code how to upload a file in chrome extension – Rahul shukla May 04 '18 at 07:03
  • You may refer with this [thread](https://stackoverflow.com/questions/24886628/upload-file-inside-chrome-extension). Try to use the [FileReader API](https://developer.mozilla.org/en-US/docs/Web/API/FileReader) which lets web applications asynchronously read the contents of files (or raw data buffers) stored on the user's computer, using `File` or `Blob` objects to specify the file or data to read. You may also check this [link](https://stackoverflow.com/questions/21132971/upload-file-as-a-form-data-through-chrome-extension) for additional reference. – abielita May 04 '18 at 15:49

0 Answers0