Pergunta

So I'm basically developing an automated click-paste-and-upload system for mutiple texts and files inside a google page.

This method helped me get the instances of objects that I'm looking for: buttons, textboxes, richtextboxes, etc.

Now I want to work with them.

So for example I know the id of a button , and the function subscribed to its click event. How do I trigger the click event from the extension ? I've tried injecting a script with the click event handler (discovered with DOM inspector) at "document_startup" but I don't get an error or anything else.

Here's the content script! The loggerhead function should have inserted the script but I don't think it did. What might be the reason for the blow code not giving anything?

// Runs a function for every added DOM element that matches a filter
// filter -- either function(DOM_node){/*...*/}, returns true or false 
//           OR a jQuery selector
// callback -- function(DOM_node){/*...*/}
function watchNodes(filter, callback){
  observer = new MutationObserver( function (mutations) {
    mutations.forEach( function (mutation){
      if(typeof filter === "function"){
        $(mutation.addedNodes).filter(
          function(i){ return filter(this); }
        ).each(
          function(i){ callback(this); }
        );
      } else {
        $(mutation.addedNodes).filter(filter).each(
          function(i){ callback(this); }
        );
      }
    });
  });

  // For every added element, a mutation will be processed
  //   with mutation.taget == parent
  //   and mutation.addedNodes containing the added element
  observer.observe(document, { subtree: true, childList: true });
}


function loggerhead(node) {
    console.log("passhead"); 

    //also inject jquery
    var jqueryEl = document.createElement('script');
    jqueryEl.setAttribute('src', chrome.extension.getURL('jquery-1.11.1.min.js'));
    jqueryEl.setAttribute('type', 'text/javascript');

    var scriptEl = document.createElement('script');
    scriptEl.setAttribute('src', chrome.extension.getURL('script.js'));
    scriptEl.setAttribute('type', 'text/javascript');

    node.appendChild(jqueryEl);   
    node.appendChild(scriptEl); 
}


watchNodes("head", loggerhead);


// method not working
//var gmailHead = jQuery("head", document).get(0);

script.js contains the function of subscribed to the click event of the button that I've managed to find through the DOM inspector:

function Cdb(b){return function(){if(Vbb()){return Ddb(b,this,arguments)}else{var a=Ddb(b,this,arguments);a!=null&&(a=a.val);return a}}}
Foi útil?

Solução

You should try to call the existing click handler like

buttonElement.click()
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top