单击,粘贴文本并从扩展程序上载文件

时间:2014-05-14 05:22:46

标签: google-chrome google-chrome-extension

因此,我基本上为谷歌页面内的多个文本和文件开发了一个自动点击粘贴和上传系统。

这个method帮助我获取了我正在寻找的对象实例:按钮,文本框,富文本框等。

现在我想与他们合作。

所以例如我知道按钮的id,并且函数订阅了它的click事件。如何从扩展程序中触发click事件?我已经尝试使用click事件处理程序(使用DOM检查器发现)在" document_startup"中注入脚本。但我没有得到错误或其他任何错误。

这是内容脚本! loggerhead 功能应该已经插入了脚本,但我认为它没有。什么原因可能是打击代码没有给出什么?

// 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 包含订阅我通过DOM检查器设法找到的按钮的点击事件的功能:

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}}}

1 个答案:

答案 0 :(得分:1)

您应该尝试调用现有的点击处理程序,如

buttonElement.click()
相关问题