Appeler des fonctions JS depuis un champ de texte avec extension Chrome
Je suis entrain de créer ma première extension chrome et j'ai des soucis pour appeler des fonctions JS depuis la page index html, je pense que le problème c'est au niveau du code background.js, voici les composants de mon extension:
manifest.json
----------------
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| {
"name": "Zero Retard",
"version": "1.0",
"manifest_version": 3,
"description": "Auto fill form GRC",
"icons": {
"16": "icon/icon.png",
"48": "icon/icon.png",
"128": "icon/icon.png"
},
"action": {
"default_popup": "index.html",
"default_icon": "icon/icon.png"
},
"options_page": "options.html",
"content_scripts": [
{
"matches" : [
"http://*/*",
"https://*/*"
],
"js": ["jquery-3.6.0.min.js", "injector.js"]
}
],
"permissions": [
"clipboardWrite",
"activeTab",
"notifications",
"contextMenus",
"storage",
"scripting",
"tabs"
]
} |
index.html
-------------
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <!doctype html>
<html>
<head>
<style>
html, body {
height: 80px;
width: 200px;
}
</style>
</head>
<body>
<h1>GRC</h1>
<form>
<div>
<label for="example">Veuillez saisir un code</label>
<input name="inpt" id="inpt"/>
<button id="btn" onClick="onClickHandler( document.getElementById('inpt').value )">Enter</button>
<script src="background.js"></script>
</form>
</div>
</body>
</html> |
background.js
----------------
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| document.addEventListener('DOMContentLoaded', function () {
// Get button by ID
var button = document.getElementById('btn');
button.onclick = document.getElementById('inpt').value;
});
async function onClickHandler(f){
functions[f]();
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['injector.js']
});
window.close();
} |
injector.js
----------------
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| functions = {
1 : function(){alert(1);},
2 : function(){alert(2);},
3 : function(){alert(3);},
4 : function(){alert(4);},
5 : function(){alert(5);},
6 : function(){alert(6);},
}
function onClickHandler(f){
functions[f]();
} |