L
L
lightalex2015-10-04 20:17:53
JavaScript
lightalex, 2015-10-04 20:17:53

How to call an event on a page using a Chrome extension?

Hello!
I am writing an extension for Chrome
The extension should automatically fill out the form on the page and submit it when the button in the popup's

manifest.json is clicked:

{
    "manifest_version": 2,
 
    "name": "Time to bot it",
    "description": "So many pages",
    "version": "1.0",
  
    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },

  "permissions": [
    "tabs", "http://*/*", "https://*/*", "background", "browsingData", "contextMenus", "cookies"
  ]
}


popup.html:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Time to bot it!</title>
    <script src="jquery.js"></script>
    <script src="script.js"></script>
  </head>
  <body>
    <input type="button" class="bt" value="Button"/>
    <div class="res"></div>
  </body>
</html>


script.js (included in popup.html):
$(document).ready(function(){
  $('input').click(function(e){
    chrome.tabs.executeScript(null, {file:"click.js"});
  });
});


click.js:
$(document).ready(function(){
  $('input').trigger('click');
});


Current page code:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="jquery.js"></script>
    <script>
      $(document).ready(function(){
        $('.bt').click(function(){
          $('.num').html($('.num').html()*1+1);
        });
      });
    </script>
  </head>
  <body>
    <input type="button" class="bt" id="bt" value="Click!"/>
    <div class="num">0</div>
  </body>
</html>


Error in console on current page:
Uncaught ReferenceError: $ is not defined

Question - what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lightalex, 2015-10-04
@lightalex

The issue was resolved by itself
In the manifest, it was necessary to connect jquery to the page, since the page scripts are not touched when accessed from the extension

"content_scripts":[{
  "matches": ["http://*/*", "https://*/*"],
  "js": ["jquery.js"]
}]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question