The documentation makes me believe that the two should be equivalent.
declarative:
In the manifest:
Enter code here... "js": ["jquery-3.4.1.min.js","jquery-ui.min.js","content.js","test_addtl_content.js"]
in both content.js and test_addtl_content.js, the first statement is a console.log ("in content.js" and "in addtl_content.js", respectively), and when the extension load on a page, I can see them in the dev console. An object defined in test_addtl_content.js is visible in the dev window when selecting the extension's context.
programmatic (I have both tabs and activeTab permissions set):
manifest:
"js": ["jquery-3.4.1.min.js","jquery-ui.min.js","content.js"]
content.js:
chrome.extension.sendMessage({message:'runContentScript', contentScript: "test_addtl_content.js"},function (r) {
if ( r ) {
hdnLog("hdn: runContentScript status " + r.status);
}
});
background script:
} else if ( request.message == "runContentScript" ) {
console.log("runContentScript called, with contentScript " + request.contentScript);
chrome.tabs.executeScript(
null,
{
file: request.contentScript
},
function () {
console.log("runContentScript executeScript callback");
}
);
sendResponse({status:"done"});
}
With this setup, I do get the status from send message in the page's dev console, and I do get the console.log messages (in the handler and in the execute script callback) in the background page inspection dev console, but the test_addl_content.js has not executed in the page itself: the console.log is not shown and an object in that file is not defined in the page in either the top or extension context.
In the background page I do get this seemingly unrelated error:
I do not see this message when using the declarative method, so it must be related somehow.
} else if ( request.message == "runContentScript" ) {
var contentScriptUrl = chrome.runtime.getURL(request.contentScript);
console.log("runContentScript called, with contentScript " + request.contentScript + ", url: " + contentScriptUrl);
chrome.tabs.executeScript(
null,
{
file: contentScriptUrl
},
function () {
console.log("runContentScript executeScript callback");
}
);
sendResponse({status:"done"});
}