Chrome Extension Create a Tab and inject content into it

Viewed 227

Hi I'm trying to create a new tab in my background script, and inject a script (for now its just a regular alert to check the code), but I cant manage to do that since CSP is blocked if I try inline script and content scripts cant be injected into the chrome-extensions:// scheme.

my manifest:

{
    "manifest_version": 3,
    "name": "OSINTisizer",
    "description": "OSINT any IP or URL",
    "version": "1.20",
    "icons": {
        "48": "images/logo_48.png",
        "128": "images/logo_128.png"
    },
    "background": {
        "service_worker": "background.js"
    },
    "permissions": [
        "contextMenus",
        "background",
        "scripting",
        "tabs",
        "storage",
        "activeTab"
    ],
    "host_permissions": [
        "*://*/*"
    ]
}

My background.js code snippet that is relevant:

chrome.tabs.create({ url: 'report.html' });
    });

I'd like the report.js code to be injected into the newly created tab under:

chrome-extensions://foobar/report.html

UPDATE: Trying to do this task using chrome.scripting.executeScript on the newly created tab but still not wroking

chrome.tabs.create({ url: "report.html" }, function (createdTab) {
            chrome.scripting.executeScript({
              target: { tabId: createdTab },
              files: ["report.js"],
            });
          });
1 Answers

Solved!! For some reason the CSP blocked me the previous time I tried to do that but the code that worked is :

chrome.storage.local.set({ info: uniqeEmails }, function () {});
          chrome.tabs.create({ url: "report.html" },{
<!DOCTYPE html>
<html>
<head>
    <script src="report.js"></script>
</head>
<body>
</body>

</html>
Related