1

I want to call a function in the chrome.tabs.executeScript that returns a string that I will be sending it in chrome.runtime.sendMessage().

code:

chrome.tabs.executeScript(tab.id, {code:var string1= ??? ;
chrome.runtime.sendMessage(string1);"});

How do I do this . My function contains Javascript code that returns a string or an array of strings.

Some help would be appreciated.

Update:

function(array_of_Tabs) {
  var tab = array_of_Tabs[0];
  //tab.url; - url of the active tab

  chrome.tabs.executeScript(tab.id, {code: 'var test = document.body,text= test.textContent,bodyStart="<BODY",bodyEnd="</BODY>",regex=bodyStart+"(.*?)"+ bodyEnd,regexg = new RegExp(regex, "g"),match = regexg.exec(text);' + 'chrome.runtime.sendMessage(match);'});
});
chrome.runtime.onMessage.addListener(function(request) {
  document.getElementsByTagName('html')[0].innerHTML = request;
});

This should've worked according to what you told me, But doesn't . Why.?

And yes BTW I am not parsing pure html on the page it may be anything say to for that matter.

1

1 Answer 1

3

chrome.tabs.executeScript has a third, optional argument that has access to the result of your injected code. E.g.

// In `background.js`:
...
const tab = ...
chrome.tabs.executeScript(
    tab.id,
    {code: 'const string1 = "Hello, world!"; return string1;'},
    resultArr => processString1(resultArr[0]));

function processString1(str1) {
  alert(`String1 = "${str1}"`);
}

If this does not cover your needs and you still want to use Message Passing:

// In `background.js`:
...
const tab = ...
chrome.tabs.executeScript(
    tab.id,
    {
      code: 'const string1 = "Hello, world!"; ' +
            'chrome.runtime.sendMessage({text: string1});',
    });

chrome.runtime.onMessage.addListener(msg => {
  if (msg.text !== undefined) {
    alert(`String1 = "${msg.text}"`);
  }
});
Sign up to request clarification or add additional context in comments.

9 Comments

It does not work because you changed the code and introduced errors. I don't have any better advice to give than my comment in your other identical question.
I really don't find much help about injecting a file from the docs mate :(
It's OK. It would make the code more readable, but it wouldn't work anyway. Stop trying to parse HTML with regex, read about textContent read the Chrome Extension docs more carefully and you'll be good to go.
see I am not parsing html but some text on the page which can be any delimiter's instead of those tags.
I really don't understand why you don't want to admit that you are trying to parse HTML with RegEx, but it on't help you in any way. I don't have anything more to say. I believe you should post a new question, state as clear as possible what you are trying to achieve and hope for some good advice to follow.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.