Execute the code received as a string is possible.
Option 1.
Use the eval function. It executes code presented as a string. The string, in turn, may contain javascript statements.
eval("alert('Stackoverflow')");
It is necessary to use the function carefully, as there is a probability of execution of malicious code.
I do not know where the use of this function would be justified.
Option 2.
Use the chrome.tabs.executeScript function. She inserts the code passed to her on the page.
chrome.tabs.executeScript(tabId, { code: 'document.body.style.transform = "scaleX(1)";' }, function(result) { // Something}
In this example, we pass the functions:
ID of the tab into which the code should be inserted. Optional.
an object describing the code to be inserted. In the example, only the code property containing the code as a string is passed. You can specify the name of the script, but instead of the code property should be file . It has other properties.
callback function that will work after the code is inserted into the page. Optional.
Option 3.
Instead of passing lines of code, send messages that signal what needs to be done. After receiving the message, some method is executed.
For example, we received a message from the background , checking the repository where the actions with the messages associated with them are stored.
{"getItem": false} . We check the store and look for a method that is associated with the getItem message. If we find - we call it and we transfer it data.
You can post several actions on one message.
I think this option is best suited.
You can not write your own functionality for storing actions, etc., but use the window object:
window["getItem"](param);
But here a few actions to pull will not work.
eval | executeScript