Chrome扩展程序自动点击元素

时间:2018-10-31 14:35:22

标签: google-chrome-extension

我正在开发一个Chrome扩展程序,当我访问“ https://www.google.com/”时会执行这些操作

  1. 用“ JAV”填充搜索栏;这有效
  2. 然后自动单击搜索按钮;即使在我尝试自动点击的任何元素上,这也不起作用。

清单文件:

{
  "manifest_version": 2,

  "name": "google extension",
  "version": "0.1.0",
  "description": "google extension",

  "browser_action":{
  "default_icon":"IMG/ghost.png"
  } , 

  "content_scripts": [{
  "js": ["JS/jquery.min.js","JS/changecolor.js"],
  "matches": ["https://www.google.com/*"],
  "run_at": "document_end"
}]

}

changecolor.js文件:

$("#tsf").css("background","black"); // this works
$("#lst-ib").val("JAV"); // this works
$(".jsb").eq(2).find("input").click(); //this doesnt work

我想念什么吗?

1 个答案:

答案 0 :(得分:0)

我已经找到用户PAEz的答案

How can I make a Chrome extension automatically click a button when a page loads?

我要在这里引用它:

function simulateClick(obj) {
  var evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window,
  0, 0, 0, 0, 0, false, false, false, false, 0, null);
  var canceled = !obj.dispatchEvent(evt);      
  /*

  if(canceled) {
  // A handler called preventDefault
  alert("canceled");
  } else {
  // None of the handlers called preventDefault
  alert("not canceled");
}
*/
}

var what = document.querySelector('input[type="button"][value="Start!"]');
simulateClick(what);