0

I have looked at a lot of different posts about using execute script and send message to execute the content script on a specified tab, but it doesn't execute the content script until I do a hard refresh and then the response from the content script is successful. Attached below is the call back function for button clicked this is in a popup.js file.

    function buttonClicked() {
    // Get an object for the active tab
  console.log("print dymo has been clicked");
    chrome.tabs.query({active: true, currentWindow: true}, function(tab_array){
        // send a messege to the contentscript that will then scrape the web
    // it will then call the popup.js receiveURL() method with the url it makes
    console.log("Messege sent to conntent script");
    alert("print has been clicked")
    alert(tab_array[0].id)
        chrome.tabs.sendMessage(tab_array[0].id, {getHTML: true}, receiveURL);
    });

Content Script:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {

  // When there is a request (that will come in as true)
 if (request) {

   /*********************************************************************
    IF YOU NEED TO CHANGE THE WEB SCRAPER START HERE
   **********************************************************************/

   // Take out everything in the html before the tag "<label>Job:</label>"
   // Selects only the inner elements between the style row
   var matches = document.querySelectorAll('ul.list-unstyled')
   //Gives us the inner text of the elements of information
   //Gives us an array of all the information split up by new lines
   information = matches[1].innerText.split(/\r?\n/)

   //Iterate ansd store everything in a dictionary split by a : 
   var dict_info = {}
   for (index = 0; index < information.length; index++) { 
    parts = information[index].split(": ")
    ans = ""
    for(i = 1; i < parts.length; i++) {
      ans += parts[i]
    }
    dict_info[parts[0]] = ans 
  }
  var name =  dict_info['Requestor']
  var job = dict_info['Job']
  if (job != undefined) {
    job = job.match(JOB_REGEX)[1]
  }
  var request = dict_info['Request']
  if (request != undefined) {
    request = request.match(REQ_REGEX)[1]
  }
  var file = dict_info["File"]
  if (file.length > 10) {
    file = file.substring(0,file.length-9)
  }
  if (file.length > 20) {
    file = file.substring(0, 20)
  }
  var email = dict_info['Requestor Email']
  var cost = dict_info['Estimated Cost']
  if(cost == undefined) {
    cost = dict_info['Cost']
  }
  if (cost != undefined) {
    cost = cost.match(COST_REGEX)[1]
  }
  name = name.split(" ")
  name = name[0] + "/" + name[name.length-1]
  var url = "https:// test" 


     sendResponse(url);
     return true;
    }
  }
);
  • 1) content scripts declared in manifest.json run only when the page is initially loaded or refreshed so it sounds like you need to remove `content_scripts` secton completely and switch to [programmatic injection](https://developer.chrome.com/extensions/content_scripts#functionality), 2) no need for "return true". – wOxxOm Dec 06 '19 at 19:21
  • @wOxxOm if I were to do that how would I pass back the url to the button clicked function? – Shlok Khandelwal Dec 06 '19 at 19:33
  • Just like you do currently (messaging) or directly from executeScript ([example](/a/57176106)). – wOxxOm Dec 07 '19 at 05:08

0 Answers0