V
V
Vitaly2017-01-04 20:08:47
JavaScript
Vitaly, 2017-01-04 20:08:47

How to make javascript code work in chrome extension?

All the best! Here I am trying to write my extensions for chrome, but I have a problem with the script.
here is my manifest:

{
    "manifest_version": 2,

    "name": "test", // Название расширения
    "version": "1.0", // Номер версии
    "description": "for test", // Описание расширения

    "permissions": [
        "tabs","http://*/*" 
    ],
    "content_scripts": [
    {
      "matches": [ "*://*/*" ],
      "js": ["main.js"] .  // мой js файл
    }
  ],


    "browser_action": {
        "default_title": "test", // Название кнопки
        "default_icon": "images/icon.png", // Иконка для кнопки
        "default_popup": "popup.html" // Всплывающее окно
    },

    "options_page": "options.html" // Страница настроек
}

popup.html :
<!doctype html>
<html>
  <head>
    <title>test</title>
  </head>
  <body>
  <script src="main.js"></script><!-- скрипт, выполняющийся при нажатии на иконку расширения-->
  </body>
</html>

main.js:
let a = document.getElementById('firstHeading').innerText;
console.log(a);

I'm trying, by clicking on the extension icon, to execute my own script that will take the data from the open page in the browser and process it (for a start, I output it to console.log).
As a result, the code that I described above executes the code from the main.js file when a new window is opened, but nothing happens when the icon is clicked. Please tell me what is wrong

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nick_Apex, 2017-01-07
@Nick_Apex

Try
document.querySelector('a[id=firstHeading']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question