0

Well, I'm stuck in this question because I don't even know where to start to searching.

Here is the problem I want to solve:

I would like to automatize some process that requires "copy-paste" values from one site to another (with different URLs) so I would like to do an extension in which I can locate a DOM node and take its text value and automatically put in a textfield from another page.

I searched in Mozilla oficial documentation and found that "tabs.sendMessage" could be something I could use but I need the tabs.id and for what I've read "chrome.tabs" appears to be a property only from extensions, so I'm starting to think that maybe there is no way to do this.

I would like to learn how to do this because It would save me hours from routine job that requires to pass data from one tab to another but change some stuff.

3
  • You need a content script with matches for one site and another content script with matches for another one. The first one will find the node and save its value in chrome.storage.local. The second one will read it at its start using chrome.storage.local.get and also use chrome.storage.onChanged listener to read the new value while it runs.
    – woxxom
    Commented Nov 18, 2021 at 8:08
  • Thanks @wOxxOm it worked out, I would like to put your comment as the right answer but I can't, I guess is because is a comment. Sorry to answer until now, I didn't understand well how matches and content scripts work and I took this weekend to finally get something that worked. Commented Nov 22, 2021 at 18:40
  • My comment is just a hint so it would be helpful if you add an answer yourself with the code.
    – woxxom
    Commented Nov 22, 2021 at 18:41

1 Answer 1

3

Thanks to @wOxxOm i found a way to get trought this problem, here is the solution that I get:

An action popup that starts script on the page from what I'm taking the information and a content script that runs on the destination page, here is the manifest.json:

{
  "name": "autofill",
  "description": "Writes info from one tab to another",
  "version": "1.0",
  "manifest_version": 3,
  "background": {
    "service_worker": "first-page-background.js"
  },
  "permissions": ["storage", "activeTab", "scripting"],
  "content_scripts": [
    {
      "matches": ["https://second-tab.com/"],
      "js": ["second-tap-content.js"]
    }
  ],
  "action": {
    "default_popup": "popup.html"
  }
}

Here is the javascript linked to the "popup.html" in which I change the chrome storage variable:

document
.querySelector('xpath_to_element_i_want_trigger_action') 
.addEventListener('click', () => {
  let data = document.querySelector(
    'xpath_to_data'
  ).innerText;
  chrome.storage.sync.set({ form_data: data });
});

And here is the running code in the script content for the second tab:

chrome.storage.onChanged.addListener(function (changes) {
  for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
    
    let searchBox = document.querySelector("#id_in_form)

    searchBox.innerHTML = newValue

  }
});

I hope this helps anyone else!

2
  • 1
    I'm also attempting to get data from one open tab and pass the data to another open tab (different domains). Thanks for sharing this code. Just wondering though, did you need to put anything in "first-page-background.js"? Also, 8 months later how is this code working out for you, any issues? Commented Jul 16, 2022 at 19:07
  • 1
    Hi man, yes. In my case I put the request to send info to a page domain to which I need it to send the info. But I think (I don't remember very well how extensions worked) that at first time it wasn't necesary and I didn't write anything on the "page-background.js" file. I did it because the request was need it to come from that part of the extension for secure reasons, but I think it is not necesary. For the working of the code it is running good and in several computer too :) Commented Jul 17, 2022 at 2:22

Your Answer

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

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