0

Quick summary

The code below is a file called popup.js. It listens for a click, and sends a message to background.js. Background.js executes another script, and a variable is created.

I somehow need to pass this variable back to popup.js, and continue within the userHasClicked function. The way it is now the response I get is "undefined", and there's nowhere to go from there.

var theParent = document.querySelector("#MENY");
theParent.addEventListener("click", userHasClicked, false);

function userHasClicked(e) {
  if (e.target !== e.currentTarget) {
      var clickedItem = e.target.id;
      chrome.runtime.sendMessage({type: "ResponseType", directive: clickedItem}, function(response) {
        console.log(response);
        this.close();
      });
      };
  e.stopPropagation();
}

Routine:

  1. User clicks on an option in popup.html
  2. Event("click") -> sendmessage("type of click")
  3. background.js listens for the message, and executes content.js
  4. content.js creates the variable and can send it back to anyone who listens.

The problem:

  1. The variable must come as a response argument to step 2 (within eventloop)
  2. Save the response as variable.
  3. document.execCommand("copy").
  4. Done

This code below is the relevant part of background.js.

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        switch (request.type) {
                case "ResponseType":
                    var LoggType = request.directive;
                    console.log(LoggType)
                    chrome.tabs.executeScript(null, {
                        code: 'var LoggType = "'+LoggType+'";'
                    }, function() {
                        chrome.tabs.executeScript(null, {file:"content.js"});
                    });
                    chrome.runtime.onMessage.addListener(function(req, snd, sndRes) {
                        if (req.type = "LogIsGenerated") {
                            var Logg = req.directive;
                            console.log(Logg);
                        } sndRes({});
                    });
                    if (typeof Logg !== "undefined") {
                        alert("Feedback from content.js received");
                        sendResponse({type: "FinalVar", directive: Logg});
                    }
                    else { 
                        alert("No feedback received");
                        sendResponse({});
                    };
                    break};

        return true;
    }
);    

content.js ends with this line.

chrome.extension.sendMessage({type: "LogIsGenerated", directive: Logg});

Logg is the variable containing the text string I want to add to the clipboard.

At the moment this the code below alerts for No feedback received.

          chrome.runtime.onMessage.addListener(function(req, snd, sndRes) {
                    if (req.type = "LogIsGenerated") {
                        var Logg = req.directive;
                        console.log(Logg);
                    } sndRes({});
                });
                if (typeof Logg !== "undefined") {
                    alert("Feedback from content.js received");
                    sendResponse({type: "FinalVar", directive: Logg});
                }
                else { 
                    alert("No feedback received");
                    sendResponse({});
                };
0

1 Answer 1

0

Rob W presented a solution here: Background script can write to clipboard in a very simple manner

Worked excellently.

Not the answer you're looking for? Browse other questions tagged or ask your own question.