I have an extension which operates a script on a specific page that gets dynamic data.
For this reason, the very last line of the script execution instruct the page to reload.
The Chrome Extension itself is not too complicated (I did not add background.js and I'd prefer not to), having pretty much just the script and a popup with some buttons to trigger the script to work under some conditions (i.e. user modify some values in a JSON object, confirm them and, using chrome.storage.local send them as a an array of string to the script which uses it for its calculations).
Long story short: what I'm looking for is a way to tell the popup it has to trigger the script a certain amount of time (say, 5) without the user to re-trigger the script launch.
Currently, simplyfing, my popup.js is this:
current popup.js
var toBeChangedByUser = //string
const varAsJSON = JSON.parse(toBeChangedByUser);
//function that will put varAsJSON in the chrome.local.storage
function submitButton() {
console.log("submit button clicked in popup");
asyncCallbackSendMessage("submitButton");
}
function asyncCallbackSendMessage(messageString) {
chrome.tabs.query({
active: true,
currentWindow: true,
}, function (arrayOfTabs) {
var activeTab = arrayOfTabs[0];
var activeTabId = activeTab.id;
chrome.tabs.sendMessage(activeTabId, {
"message": messageString
});
});
The only thing I was able to think of is using a for loop and try something like this:
might-work popup.js?
var toBeChangedByUser = //string
const varAsJSON = JSON.parse(toBeChangedByUser);
//function that will put varAsJSON in the chrome.local.storage
function submitButton() {
console.log("submit button clicked in popup");
var i=0;
for (i; i<5; i++) {
setInterval(console.log("short break"), 500); //allowing time within a click and the following one
document.addEventListener('DOMContentLoaded', console.log("loop no. " + i + " starting")); //wait page full reloading before next step
asyncCallbackSendMessage("submitButton");
}
}
function asyncCallbackSendMessage(messageString) {
chrome.tabs.query({
active: true,
currentWindow: true,
}, function (arrayOfTabs) {
var activeTab = arrayOfTabs[0];
var activeTabId = activeTab.id;
chrome.tabs.sendMessage(activeTabId, {
"message": messageString
});
});
I tried this once and it launched this error:
Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' blob: filesystem:".
However, I'm not a big fan of setting intervals so I'm wondering if Chrome Extensions offer some better solutions to repeat a script that at the end of their run will reload the page they operates in.
Is there something that could do the trick?