Answer the question
In order to leave comments, you need to log in
How to call a function in a Chrome extension?
Good day to all! Decide to try writing an extension for Google Chrome. popup wrote:
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="./css/style.css">
<script src="/js/popup.js"></script>
</head>
<body>
<header>
<div class="logo">
<div class="logo__img">
</div>
<p class="logo__text">Ad Scout</p>
</div>
<div id="on" class="on" onclick="hov_on(this)">
<div class="on__img">
<img src="./img/fire-black.svg" alt="">
</div>
<p class="on__text">Run</p>
</div>
</header>
</body>
</html>
{
"name": "",
"description": "",
"version": "1.0",
"manifest_version": 2,
"icons": {
"16": "logo.png",
"48": "logo.png",
"128": "logo.png"
},
"browser_action": {
"default_title": "",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": [ "https://yandex.ru/*", "https://www.google.ru/*" ],
"js": [ "script.js" ],
"run_at":"document_end"
}
]
}
Answer the question
In order to leave comments, you need to log in
Due to the default CSP, inline js does not work in the popup. Use addEventListener in popup.js
Moar info
Having smoked a little manulchik , I solve the problem.
In general, js files that will work with popup content should be called popup.js and it is important to include it before the closing body tag
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Ads Scout</title>
<link rel="stylesheet" href="./css/style.css">
</head>
<body>
<header>
<div class="logo">
<div class="logo__img">
</div>
<p class="logo__text">Ad Scout</p>
</div>
<div id="on" class="on">
<div class="on__img">
<img on="0" src="./img/fire-black.svg" alt="">
</div>
<p class="on__text">Зажигаем?</p>
</div>
</header>
<script src="/js/popup.js"></script>
</body>
</html>
let on = document.getElementsByClassName('on')[0];
on.onclick = function(element) {
let img = on.children[0].children[0];
let work = img.getAttribute('on');
if (work == 0) {
img.src = './img/fire-hov.svg';
img.setAttribute('on', 1);
}
else {
img.src = './img/fire-black.svg';
img.setAttribute('on', 0);
}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question