Sto provando a fare un'estensione molto semplice per chrome ma sono bloccato a passare una variabile da popup html.Estensione Chrome: passa le variabili da popup html
Questo è il codice che ho finora:
Manifest:
{
"background": {
"scripts": [ "background.js" ]
},
"browser_action": {
"default_icon": "img/test.png",
"default_popup": "popup.html",
"default_title": "Auto Clicker"
},
"description": "Auto click",
"manifest_version": 2,
"name": "Auto Clicker",
"permissions": [ "activeTab" ],
"version": "0.0.1"
}
background.js
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
switch (request.directive) {
case "popup-click-1":
// execute the content script
chrome.tabs.executeScript(null, { // defaults to the current tab
file: "myscript.js", // script to inject into page and run in sandbox
allFrames: true // This injects script into iframes in the page and doesn't work before 4.0.266.0.
});
sendResponse({}); // sending back empty response to sender
break;
}
}
);
MyScript.js
function foo(){
document.getElementById('submit-button').click();
}
setInterval(function(){
foo()}, 20000)
foo();
popup.js
function clickHandler(e) {
chrome.extension.sendMessage({directive: "popup-click-1"}, function(response) {
this.close(); // close the popup when the background finishes processing request
});
}
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('submit').addEventListener('click', clickHandler);
})
popup.html
<html>
<head>
<title>test</title>
<script src="popup.js"></script>
</head>
<body>
test
<form>
<input id="1" type = "text">
</br>
</form>
<button id='submit'> etst </button>
</body>
</html>
Finora corro foo() quando si fa clic il presentare ma ton, che viene eseguito ogni 20 secondi. Quello che voglio ottenere è aggiungere un numero al popup html e quindi usare quel numero su myscript.js per impostare il tempo della funzione setInterval.
ecco uno scenario:
apro la pagina, mi ha colpito il pulsante di estensione. C'è un popup con un modulo. Ho messo 30000 e poi ho colpito il sumbit. In questo modo, foo() verrà eseguito ogni 30 secondi.
1. Non è chiaro quale sia il problema. 2. In questo caso non vedo la necessità di script di sfondo (in realtà dubito che il tuo schema funzioni affatto) – wOxxOm
@wOxxOm 1) Voglio ottenere la variabile che l'utente inserisce nel form popup.html e sostituire il numero 20000 con quel numero, su myscript.js 2) Il codice funziona perfettamente, l'ho provato così tante volte. Quando premi il pulsante di invio su popup.html, il # pulsante di invio viene cliccato ogni 20 secondi. –