Skip to content

Commit

Permalink
feat: embed Overlay as static view in npmjs.com (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
baruchiro authored May 15, 2023
1 parent 70c8c86 commit 1ad422d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 12 deletions.
27 changes: 16 additions & 11 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,18 @@ async function buildPopup(outputDirPath) {
});
}

async function buildContentScript(contentFilePath, outputDirPath) {
const bundle = await rollup({
input: contentFilePath,
});
await bundle.write({
dir: outputDirPath,
format: 'iife',
});
}

async function buildBrowserExtension(browserType, version, fileExtension) {
let outputDirPath = path.join(distDirPath, browserType);
const outputDirPath = path.join(distDirPath, browserType);

// --------------
// icons dir
Expand All @@ -105,7 +115,7 @@ async function buildBrowserExtension(browserType, version, fileExtension) {

// --------------
// manifest.json
let manifestFilename = `manifest.${browserType}.json`;
const manifestFilename = `manifest.${browserType}.json`;
await gulp
.src(path.join(srcDirPath, manifestFilename))
.pipe(rename('manifest.json'))
Expand All @@ -114,18 +124,13 @@ async function buildBrowserExtension(browserType, version, fileExtension) {
.pipe(gulp.dest(outputDirPath));

// --------------
// content.stackoverflow.js
let bundle = await rollup({
input: path.join(srcDirPath, 'content', 'content.stackoverflow.js'),
});
await bundle.write({
file: path.join(outputDirPath, 'content.stackoverflow.js'),
format: 'iife',
});
// content script
buildContentScript(path.join(srcDirPath, 'content', 'content.stackoverflow.js'), outputDirPath);
buildContentScript(path.join(srcDirPath, 'content', 'content.npmjs.js'), outputDirPath);

// --------------
// background.js
bundle = await rollup({
const bundle = await rollup({
input: path.join(srcDirPath, 'background', 'background.js'),
plugins: [commonjs(), nodeResolve()],
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"lint:fix": "yarn lint --fix",
"lint:lockfile": "lockfile-lint --path yarn.lock --allowed-hosts npm yarn --validate-https --validate-package-names --validate-integrity --empty-hostname false",
"lint:firefox": "yarn build && web-ext lint --source-dir ./dist/firefox",
"__start": "web-ext run --devtools --start-url https://stackoverflow.com/questions/33527653",
"__start": "web-ext run --start-url https://www.npmjs.com/package/node-sass",
"start:chrome": "yarn __start --source-dir ./dist/chrome --target chromium",
"start:firefox": "yarn __start --source-dir ./dist/firefox"
},
Expand Down
22 changes: 22 additions & 0 deletions src/content/content.npmjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { mountContentScript } from './content';
import { fetchPackageInfo } from './content-events';
import { urlParsers } from './registry/npm';

const addPackageReport = (packageID) => {
const packageReport = document.createElement('overlay-package-report');
packageReport.setAttribute('package-type', packageID.type);
packageReport.setAttribute('package-name', packageID.name);

const repository = document.querySelector('#repository');
if (repository) {
repository.parentElement.insertBefore(packageReport, repository);
}
};

mountContentScript(async () => {
const packageId = urlParsers[location.hostname.replace('www.', '')]?.(location);
if (!packageId) return;

addPackageReport(packageId);
fetchPackageInfo(packageId);
});
3 changes: 3 additions & 0 deletions src/custom-elements/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import '@webcomponents/custom-elements';
import { defineCustomElement } from 'vue';
import Indicator from './Indicator.vue';
import PackageReport from './PackageReport.vue';
import { initEventListenersAndStore } from './webapp-events';

initEventListenersAndStore();
Expand All @@ -13,6 +14,8 @@ Promise.all(Object.values(modules).map((module) => module())).then((modules) =>

const indicatorCustomElement = defineCustomElement(Indicator);
customElements.define('overlay-indicator', indicatorCustomElement);
const packageReportCustomElement = defineCustomElement(PackageReport);
customElements.define('overlay-package-report', packageReportCustomElement);

console.log('Custom element defined');
});
4 changes: 4 additions & 0 deletions src/manifest.chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
{
"matches": ["*://stackoverflow.com/*"],
"js": ["content.stackoverflow.js"]
},
{
"matches": ["*://www.npmjs.com/package/*"],
"js": ["content.npmjs.js"]
}
],
"background": {
Expand Down
4 changes: 4 additions & 0 deletions src/manifest.firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
{
"matches": ["*://stackoverflow.com/*"],
"js": ["content.stackoverflow.js"]
},
{
"matches": ["*://www.npmjs.com/package/*"],
"js": ["content.npmjs.js"]
}
],
"background": {
Expand Down

0 comments on commit 1ad422d

Please sign in to comment.