M
M
Michael Kim2015-06-23 02:40:47
JavaScript
Michael Kim, 2015-06-23 02:40:47

Is it possible to do script injection on click using chrome extension?

I am writing an extension for chrome, I need to insert a script into the page with content like:
setInterval(function(){//code}, 30000); . That is, the code should be executed regardless of whether the application is open or not. What does the injection have to be done on the click of a button in the extension, is it possible? And if so, how?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2015-06-24
@Marox

Read .
The point is that when the button is clicked, a message is sent

var port = chrome.runtime.connect({name: "ClickButton"});
port.postMessage();

And on the page itself there is an event handler that is triggered when a message is received:
chrome.runtime.onConnect.addListener(function(port) {
    port.onMessage.addListener(function(msg) {
        setInterval(function(){//code}, 30000);
    }
}

Or you can run stasis code on connection.
chrome.runtime.onConnect.addListener(function(port) {
    setInterval(function(){//code}, 30000);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question