Access classes, id's, div's from a page with my extension

0
Hello, I am writing an extension with the following purpose: to 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 page DOM were empty) and I wanted a help on how I can resolve this. Detail: I already researched the subject on any dev site, including the link . Before I gave up the project I came to try a 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 );
    
asked by anonymous 17.04.2018 / 15:42

1 answer

0

Let's look at the signature of create and executeScript

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

The second argument of create , is a function of callback that receives Tab tab

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

Now of executeScript is given as a parameter of tabId , so it is netural to run executeScript on create callback.

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 id of tab , the script would run on all abas , this would pose a security risk.

    
17.04.2018 / 18:59