samedi 25 juin 2016
Running Function in Background.js from Popup.html to Inject Script
So after spending a few days trying to figure out what looks logically to be a fairly simple issue, I've decided to come here and post my issue instead.
The problem I'm facing is that I'd like to run a script in the background.js from the popup.html (using a popup.js), which in return uses one or nested setInterval loop to inject an external .js multiple times onto the current tab at set intervals. The external code works well, but I have no idea how to test the google extension, and the google extension I made does not work.
Anyway, here's the code (I've stripped down the popup.html for brevity):
Manifest.json (checked this one, it validates):
{
"background": {
"scripts": ["background.js"],
"persistent": true
},
"manifest_version": 2,
"name": "Project",
"short_name": "Script",
"version": "1.0",
"description": "Our script to do stuff",
"icons": {
"128": "icon128.png"
},
"browser_action": {
"default_icon": {
"128": "icon128.png"
},
"default_title": "Script",
"default_popup": "popup.html"
},
"permissions": [
"https://*/*",
"http://*/*",
"tabs"
]
}
Popup.html (stripped)
Small Interval: <select id="basescript-base-wait-time">
<option value="0.333">20 seconds</option>
<option value="0.5">30 seconds</option>
<option value="0.666">40 seconds</option>
<option value="1">1 minute</option>
<option value="2">2 minutes</option>
</select>
Big Interval: <select id="basescript-overall-wait-time">
<option value="5">5 minutes</option>
<option value="10">10 minutes</option>
<option value="20">20 minutes</option>
<option value="30">30 minutes</option>
<option value="60">1 hour</option>
</select>
<input id="clickgeneralscriptonce" class="button-to-input" type="button" value="Execute Single Pass"/>
<input id="clickgeneralscriptcontinuous" class="button-to-input" type="button" value="Execute Continuous"/>
<script type="text/javascript" src="popup.js"></script>
Popup.js
var bgPage = chrome.runtime.getBackgroundPage();
var intervalwait = parseFloat(document.querySelector('#basescript-overall- wait-time').value);
var basewait = parseFloat(document.querySelector('#basescript-base-wait-time').value);
document.getElementById("clickbase1script").onclick = executebase1continuous;
document.getElementById("clickgeneralscriptcontinuous").onclick = executecontinuousgeneral;
document.getElementById("clickgeneralscriptonce").onclick = executeoncegeneral;
function executeoncegeneral() {
bgPage.RunBigFunctionOnce(basewait)
}
function executecontinuousgeneral() {
bgPage.RunBigFunctionContinuous(intervalwait,basewait)
}
Background.js
//bigloop
function RunBigFunctionContinuous(interval,base) {
RunBigFunctionOnce(base)
setInterval(RunBigFunctionOnce(base),interval)
}
function RunBigFunctionOnce(base) {
executegeneral()
setinterval(executegeneral(), base) )
}
function executegeneral() { chrome.tabs.executeScript(null, {file: "injectgeneral.js"});}
The external injectable script isn't included. There are probably a number of really poor coding practices in this code, but I've just started coding last week, so I figured they could be forgiven for now.
I'm not sure if I've structured this correctly. I've tried looking at the documentation for chrome extensions and originally wanted to have the injected script as a content script, but couldn't figure out how to make content scripts work so I opted to inject it instead. Currently, when I try to execute functions from the popup, nothing happens. I feel like I might be close to getting this to work, but I have no idea where it went all wrong.
Thank you for all of your help.
Inscription à :
Publier les commentaires (Atom)
Aucun commentaire:
Enregistrer un commentaire