Skip to content

Commit 1a40680

Browse files
committed
Add optional contentScripts argument
1 parent bcd5f30 commit 1a40680

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,24 @@ async function p(fn, ...args) {
1010
}));
1111
}
1212

13-
export async function addToTab(tab = false) {
14-
if (tab === false) {
13+
export async function addToTab(tab, contentScripts) {
14+
if (typeof tab !== 'object' && typeof tab !== 'number') {
1515
throw new TypeError('Specify a Tab or tabId');
1616
}
1717

18+
if (contentScripts === false) {
19+
// Get all scripts from manifest.json
20+
contentScripts = chrome.runtime.getManifest().content_scripts;
21+
} else if (!Array.isArray(contentScripts)) {
22+
// Single script object, make it an array
23+
contentScripts = [contentScripts];
24+
}
25+
1826
try {
1927
const tabId = tab.id || tab;
2028
if (!await pingContentScript(tabId)) {
2129
const injections = [];
22-
for (const group of chrome.runtime.getManifest().content_scripts) {
30+
for (const group of contentScripts) {
2331
const allFrames = group.all_frames;
2432
const runAt = group.run_at;
2533
for (const file of group.css) {
@@ -37,10 +45,10 @@ export async function addToTab(tab = false) {
3745
}
3846
}
3947

40-
export function addToFutureTabs() {
48+
export function addToFutureTabs(scripts) {
4149
chrome.tabs.onUpdated.addListener((tabId, {status}) => {
4250
if (status === 'loading') {
43-
addToTab(tabId);
51+
addToTab(tabId, scripts);
4452
}
4553
});
4654
}

0 commit comments

Comments
 (0)