Access one-page classes, id’s, div’s with my extension

Asked

Viewed 36 times

0

Hello, I am writing an extension with the following goal: access the target elements of a page (type Document.querySelector('.Jobs')) and from there write the rest of the instructions. However, from what I read, content scripts access the DOM in an isolated environment (so when I request "DOCUMENT" return as if the DOM of the page was empty) and wanted a help on how I can fix it. Detail: I have already researched about the subject in all dev sites, including the http://developer.mozilla.org. Before giving up the project I came to try one last light here. Thanks people.

Edit: background js.

chrome.browserAction.onClicked.addListener(function(tab){
  chrome.tabs.create({active: true, url: 'https://www.sine.com.br/vagas-empregos-em-contagem-mg'}, function(){
    console.log('criada');  
  });
});
chrome.tabs.executeScript({
        file: "execute.js"
      });

execute js.

setInterval(() => {
    var vagasList = document.querySelector('.jobs').childNodes;
    n = [];

    vagasList.forEach(element => { 
      if(element.nodeName != '#text') {
        var enfia = n.push(element.children[0].children[1].href);
        var ind = n.indexOf(element);
        if (ind > 2) {
          n.splice(ind);
        }
      }
    });

    n.forEach(element => {
      console.log(element);
      open(element);
      vagas = setInterval(function(){
        document.querySelector('.pnl-candidatar').children[0].click();
        console.log('ok');
      }, 1000);  
      
    });
  }, 5000 );

  • If you post the code it is easier to get help.

  • Here are the codes

1 answer

0

let’s look at the signature of create and of executeScript

create − chrome.tabs.create(object createProperties, function callback)

the second argument from create, is a function of callback who receives the Tab tab

executeScript − chrome.tabs.executeScript(integer tabId, object details, function callback)

Now of executeScript receives as a parameter of tabId, then it is netural that the executeScript in the callback of create.

chrome.tabs.create({
  active: true, 
  url: 'https://www.sine.com.br/vagas-empregos-em-contagem-mg'
}, function(tab) {
  console.log("Tab criada", tab)
  chrome.tabs.executeScript(tab.id, {
    file: "execute.js"
  }, function (results) {
    console.log("Script executado", results)
  });
});

Without informing the id of tab, the script would run on all abas, this would pose a security risk.

  • applied correction ! Now how do I access Document.querySelector('.Jobs'). childNodes??

Browser other questions tagged

You are not signed in. Login or sign up in order to post.