0

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}}}
Community
  • 1
  • 1
kawa
  • 422
  • 4
  • 16
  • Did you provide the scripts (jQuery and script.js) as `web_accessible_resources` (see https://developer.chrome.com/extensions/manifest/web_accessible_resources) in your manifest? – devnull69 May 14 '14 at 07:30
  • Yes, I have given those to the manifest! – kawa May 14 '14 at 07:32
  • What does that mean `script.js contains the function of subscribed`? Do you mean that `Cdb()` is the method that is being called on click of a button on the web page? Why would you want to inject the method again then? – devnull69 May 14 '14 at 07:35
  • I think you are correct; I might be confusing stuff. So, the script.js code is obtained pasting the text obtained by clicking the EventListeners tab in the DOMInspector and yes it's the definition of the event handler for the click button but if I don't put it there then adding `Cdb(b);` in script.js gives me Cdb:method undefined error. Now if the code is : `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}}} Cdb(b);` I get **b is not defined** error, and if I modify `Cdb(b);` with `Cdb();` it does nothing. – kawa May 14 '14 at 07:47
  • Did you try to call the existing click handler like `buttonElement.click()`? – devnull69 May 14 '14 at 07:49
  • You deserve a GOLD medal. Thanks a bunch!!!!!!! It worked!!!! Add an answer I'll accept it. Thank you again! – kawa May 14 '14 at 07:52

1 Answers1

1

You should try to call the existing click handler like

buttonElement.click()
devnull69
  • 16,402
  • 8
  • 50
  • 61