Skip to content

Commit 2fbb6f6

Browse files
baruchirojossef
andauthored
feat: embed Overlay as static view in pypi.org (#80)
Close #75 https://user-images.githubusercontent.com/17686879/236821652-c851b860-f269-41a3-a320-3a9475810591.mp4 Waiting for #79 to be merged. --------- Co-authored-by: Jossef Harush Kadouri <jossef12@gmail.com>
1 parent 1ad422d commit 2fbb6f6

File tree

7 files changed

+65
-6
lines changed

7 files changed

+65
-6
lines changed

gulpfile.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async function buildCustomElements(outputDirPath) {
4949

5050
// --------------
5151
// Recompile without customElement: true to extract css
52-
let results = await vite.build({
52+
const results = await vite.build({
5353
build: {
5454
emptyOutDir: false,
5555
write: false,
@@ -62,14 +62,14 @@ async function buildCustomElements(outputDirPath) {
6262
plugins: [vue(), svgLoader()],
6363
});
6464

65-
let customElementsCssOutputFilePath = path.join(distDirPath, 'custom-elements.css');
65+
const customElementsCssOutputFilePath = path.join(distDirPath, 'custom-elements.css');
6666

67-
let files = results[0].output;
67+
const files = results[0].output;
6868
for (const file of files) {
6969
if (file.name !== 'style.css') {
7070
continue;
7171
}
72-
let cssFileContent = file.source;
72+
const cssFileContent = file.source;
7373
await fs.writeFile(customElementsCssOutputFilePath, cssFileContent, 'utf8');
7474
}
7575
}
@@ -127,6 +127,7 @@ async function buildBrowserExtension(browserType, version, fileExtension) {
127127
// content script
128128
buildContentScript(path.join(srcDirPath, 'content', 'content.stackoverflow.js'), outputDirPath);
129129
buildContentScript(path.join(srcDirPath, 'content', 'content.npmjs.js'), outputDirPath);
130+
buildContentScript(path.join(srcDirPath, 'content', 'content.pypi.js'), outputDirPath);
130131

131132
// --------------
132133
// background.js
@@ -148,7 +149,7 @@ async function buildBrowserExtension(browserType, version, fileExtension) {
148149
}
149150

150151
gulp.task('compile', async () => {
151-
let version = process.env['BUILD_VERSION'] || DEV_VERSION;
152+
const version = process.env['BUILD_VERSION'] || DEV_VERSION;
152153
console.log(`compiling version ${version}`);
153154

154155
await buildCustomElements(distDirPath);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"lint:fix": "yarn lint --fix",
2222
"lint:lockfile": "lockfile-lint --path yarn.lock --allowed-hosts npm yarn --validate-https --validate-package-names --validate-integrity --empty-hostname false",
2323
"lint:firefox": "yarn build && web-ext lint --source-dir ./dist/firefox",
24-
"__start": "web-ext run --start-url https://www.npmjs.com/package/node-sass",
24+
"__start": "web-ext run --start-url https://pypi.org/project/pandas/ --start-url https://www.npmjs.com/package/node-sass",
2525
"start:chrome": "yarn __start --source-dir ./dist/chrome --target chromium",
2626
"start:firefox": "yarn __start --source-dir ./dist/firefox"
2727
},

src/content/content.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const injectScriptTag = () => {
77
(document.head || document.documentElement).appendChild(script);
88
console.log('Injected script tag', script);
99

10+
// Inject the custom-element css to the head to affect the Teleport part.
1011
const link = document.createElement('link');
1112
link.rel = 'stylesheet';
1213
link.href = browser.runtime.getURL('custom-elements.css');

src/content/content.pypi.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import browser from '../browser';
2+
import { mountContentScript } from './content';
3+
import { fetchPackageInfo } from './content-events';
4+
import { urlParsers } from './registry/python';
5+
6+
const addPackageReport = (packageID) => {
7+
const packageReport = document.createElement('overlay-package-report');
8+
packageReport.setAttribute('package-type', packageID.type);
9+
packageReport.setAttribute('package-name', packageID.name);
10+
packageReport.setAttribute('stylesheet-url', browser.runtime.getURL('custom-elements.css'));
11+
12+
const sidebar = document.querySelector('.vertical-tabs__tabs');
13+
const sidebarSection = sidebar?.querySelectorAll('.sidebar-section')[1];
14+
if (!sidebarSection) {
15+
console.log('No side section found (parent of .github-repo-info)');
16+
return;
17+
}
18+
19+
sidebar.insertBefore(packageReport, sidebarSection);
20+
};
21+
22+
mountContentScript(async () => {
23+
console.log('Running content script for PyPI');
24+
const packageId = urlParsers[location.hostname.replace('www.', '')]?.(location);
25+
if (!packageId) {
26+
console.log('No package found', packageId);
27+
return;
28+
}
29+
30+
addPackageReport(packageId);
31+
fetchPackageInfo(packageId);
32+
});

src/custom-elements/PackageReport.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,30 @@ export default defineComponent({
9494
type: String,
9595
required: true,
9696
},
97+
stylesheetUrl: {
98+
type: String,
99+
required: false,
100+
},
97101
},
98102
99103
setup(props) {
100104
const packageInfo = usePackageInfo(props.packageType, props.packageName);
101105
return { packageInfo };
102106
},
103107
108+
mounted() {
109+
if (!this.stylesheetUrl) return;
110+
111+
// Pypi: CSP: The page’s settings blocked the loading of a resource at inline (“style-src”)
112+
// We need to load the stylesheet as a file, inside the custom-element.
113+
// https://github.com/os-scar/overlay/issues/75#issuecomment-1538224560
114+
const link = document.createElement('link');
115+
link.rel = 'stylesheet';
116+
link.type = 'text/css';
117+
link.href = this.stylesheetUrl;
118+
this.$el.parentNode.appendChild(link);
119+
},
120+
104121
computed: {
105122
packageLicense() {
106123
return this.packageInfo?.licenses?.[0] || '';

src/manifest.chrome.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
{
1515
"matches": ["*://www.npmjs.com/package/*"],
1616
"js": ["content.npmjs.js"]
17+
},
18+
{
19+
"matches": ["*://pypi.org/project/*"],
20+
"js": ["content.pypi.js"]
1721
}
1822
],
1923
"background": {

src/manifest.firefox.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
{
1515
"matches": ["*://www.npmjs.com/package/*"],
1616
"js": ["content.npmjs.js"]
17+
},
18+
{
19+
"matches": ["*://pypi.org/project/*"],
20+
"js": ["content.pypi.js"]
1721
}
1822
],
1923
"background": {

0 commit comments

Comments
 (0)