forked from cristianbote/bundlephobia-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for scoped packages, e.g.
@webcomponents/template
- Loading branch information
Showing
1 changed file
with
19 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,27 @@ | ||
const getPackageName = url => url.split('/').filter(Boolean).slice(0, 4).slice(-1); | ||
const getPackageName = url => { | ||
const parts = url.split('/').filter(Boolean); | ||
const [org, name] = parts[1].endsWith('npmjs.com') | ||
? parts.slice(3, 5) | ||
: parts.slice(2, 4); | ||
return org.startsWith('@') | ||
? `${org}/${name}` | ||
: name || org; | ||
}; | ||
const getBundlephobiaUrl = name => `https://bundlephobia.com/result?p=${name}&src=bundlephobia-extension`; | ||
const getIframe = () => document.querySelector('iframe'); | ||
|
||
chrome.tabs.query({ | ||
active: true, | ||
currentWindow: true | ||
}, ([currentTab]) => { | ||
const url = currentTab.url; | ||
const packageName = getPackageName(url); | ||
const iframe = getIframe(); | ||
}, ([currentTab]) => { | ||
const url = currentTab.url; | ||
const packageName = getPackageName(url); | ||
const iframe = getIframe(); | ||
|
||
iframe.onload = () => { | ||
document.body.className = ""; | ||
iframe.onload = null; | ||
}; | ||
iframe.onload = () => { | ||
document.body.className = ""; | ||
iframe.onload = null; | ||
}; | ||
|
||
iframe.src = getBundlephobiaUrl(packageName); | ||
}); | ||
iframe.src = getBundlephobiaUrl(packageName); | ||
}); |