Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create events proxy to message from webapp to contentScript #20

Conversation

baruchiro
Copy link
Collaborator

@baruchiro baruchiro commented Mar 13, 2023

fixes: #19

The finder, the code that finds packages on the page, first waits for a message from the injected script (custom-element.js) to know it loaded, then for each package it:

  1. sends a message to the background service to fetch the PackageInfo, then sends the info to the injected script (the custom-element.js)
  2. wraps the package string with <indicator>

The injected script does this on loading:

  1. Register a store.
  2. Listen to the "package info received" event, which updates the store with the package info.
  3. Send a message to the content script to notify it is ready.

The Indicator.vue component is reactive to the store.

overlay - Page 1 (1)

@baruchiro
Copy link
Collaborator Author

Suggestions for better names for files, functions, variables, different locations, organization, and architecture ARE WELCOME.

@baruchiro baruchiro force-pushed the baruchiro/How-to-hold-the-advisories-data-returned-from-the-background-service branch from ef443b0 to e0f782a Compare March 13, 2023 19:47
Comment on lines 3 to 18
let isWebappReady = false;
export const waitForWebappReady = () => {
return new Promise((resolve) => {
const interval = setInterval(() => {
if (isWebappReady) {
clearInterval(interval);
resolve(true);
}
}, 100);

setTimeout(() => {
clearInterval(interval);
resolve(false);
}, 5000);
});
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO no need to resolve(false) if you can call reject() (built-in in promise)
suggesting the following implementation with setTimeout

Suggested change
let isWebappReady = false;
export const waitForWebappReady = () => {
return new Promise((resolve) => {
const interval = setInterval(() => {
if (isWebappReady) {
clearInterval(interval);
resolve(true);
}
}, 100);
setTimeout(() => {
clearInterval(interval);
resolve(false);
}, 5000);
});
};
let isWebappReady = false;
function onAppLoaded(timeout = 5000, sleep = 100) {
let started = Date.now();
return new Promise((resolve, reject) => {
const interval = () => {
if (isWebappReady) {
resolve();
return;
}
let duration = Date.now() - started;
if(duration >= timeout){
reject(`could not load app. timeout exceeded after ${timeout} milliseconds`)
return;
}
setTimeout(interval, sleep);
};
interval();
});
}

example: https://replit.com/@jossef/wait-for-resource#index.js

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a significant difference, these are two different methods of performing the same task.

Resolved anyway.

src/content.js Outdated
Comment on lines 17 to 21
const isReady = await events.waitForWebappReady();
if (!isReady) {
console.log('Webapp is not ready, aborting');
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if resolved, assuming loaded. if rejected, assuming timeout. I would change implementation like so:

Suggested change
const isReady = await events.waitForWebappReady();
if (!isReady) {
console.log('Webapp is not ready, aborting');
return;
}
try{
await events.onAppLoaded();
catch (e){
console.error("failed to initialize content script", e);
return;
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

src/content.js Show resolved Hide resolved
@jossef jossef merged commit e6d2297 into master Mar 14, 2023
@jossef jossef deleted the baruchiro/How-to-hold-the-advisories-data-returned-from-the-background-service branch March 14, 2023 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to hold the advisories data returned from the background service?
2 participants