0

I'm working on an Chrome extension which will excute these things when I access "https://www.google.com/"

  1. Fills the search bar with "JAV"; this works
  2. Then auto click the search button ; this doesnt work, even on any element that I tried to auto click.

manifest file:

{
  "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 file:

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

Am I missing something?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 3
    Possible duplicate of [How can I make a Chrome extension automatically click a button when a page loads?](https://stackoverflow.com/questions/11387615/how-can-i-make-a-chrome-extension-automatically-click-a-button-when-a-page-loads) – Cœur Dec 31 '18 at 09:27
  • 1
    You should seriously consider not injecting jQuery. jQuery is 85kiB of minimized code. While It is possible you really *need* to load jQuery, what is shown in the question is that you are using it for the convenience of saving a couple hundred characters in your own code by not writing your code in vanilla JavaScript. If that really is all you are using jQuery for (we have no way to know), consider rewriting those 3 lines in vanilla JavaScript. One of the significant benefits of jQuery is cross-browser compatibility. That is not a consideration in a Chrome extension. – Makyen Dec 31 '18 at 23:44

1 Answers1

0

I have found the answer by user PAEz

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

I'm gonna quote it here:

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);
Cœur
  • 37,241
  • 25
  • 195
  • 267