Skip to content

Commit

Permalink
fix: Overlay didn't display "npmjs" pages that do not contain a repos…
Browse files Browse the repository at this point in the history
…itory element (#149)

fix issue 148.
#148

**content.npmjs.js** - mount Overlay based on h3 element title "install"
instead of repository element.
**utils.js** - if the element is not in the page. reject timeout after
10 seconds.
make bugs like this more easy to detect next time.
**.gitignore** - not pull cache and autogenerated files

---------

Co-authored-by: Baruch Odem (Rothkoff) <[email protected]>
  • Loading branch information
aviv1620 and baruchiro committed Aug 10, 2023
1 parent cf9b7c0 commit a274b0b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ typings/
.node_repl_history
*.tgz
.yarn-integrity
.yarn/*
.env
.env.test
.cache
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@
"lint-staged": {
"**/*": "prettier --write --ignore-unknown",
"{src,tests}/**/*.js": "eslint --fix"
}
},
"packageManager": "[email protected]"
}
4 changes: 1 addition & 3 deletions src/background/cache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import LRUCache from 'lru-cache';

const SECOND = 1000;
const MINUTE = 60 * SECOND;
import { MINUTE } from '../globals';

const _cache = new LRUCache({
max: 500,
Expand Down
9 changes: 7 additions & 2 deletions src/content/content.npmjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import { mountContentScript, reloadWhenURLChanged } from './content';
import { fetchPackageInfo } from './content-events';
import { urlParsers } from './registry/npm';

const CSS_SELECTOR_COLLABORATORS = '#collaborators';

const addPackageReport = async (packageID) => {
// remove an old package report (if exists)
const currPackageReport = document.getElementsByTagName(packageReportTagName);
if (currPackageReport?.length) {
currPackageReport.item(0).remove();
}

const repository = await waitForElement('#repository', document.querySelector('#main'));
const collaborators = await waitForElement(CSS_SELECTOR_COLLABORATORS, document.querySelector('#main'));
const packageReport = document.createElement(packageReportTagName);
packageReport.setAttribute('package-type', packageID.type);
packageReport.setAttribute('package-name', packageID.name);
repository.parentElement.insertBefore(packageReport, repository);

const properties = collaborators.parentElement.parentElement;
const install = properties.querySelector('p');
install.after(packageReport);
};

const loadPackageInfo = async () => {
Expand Down
5 changes: 4 additions & 1 deletion src/globals.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
export const SECOND = 1000;
export const MINUTE = 60 * SECOND;

export const advisories = {
snyk: 'Snyk Advisor',
depsDev: 'OpenSSF Scorecard',
socket: 'Socket',
debricked: 'Debricked',
};
export const advisoriesNames = Object.keys(advisories);

export const advisoriesNames = Object.keys(advisories);
export const indicatorTagName = 'overlay-indicator';
export const packageReportTagName = 'overlay-package-report';
10 changes: 9 additions & 1 deletion src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
* @param {Node} target A DOM Node (which may be an Element) within the DOM tree to watch for changes, or to be the root of a subtree of nodes to be watched.
*
*/
import { SECOND } from '../globals';

const waitElementTimeOot = 10 * SECOND;

const waitForElement = (selector, target) => {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('Time out to wait for the element ' + selector));
}, waitElementTimeOot);

if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
Expand Down

0 comments on commit a274b0b

Please sign in to comment.