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

load listOfInjectableURLRegex from news-sources.json #53

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alexhan01
Copy link

@alexhan01 alexhan01 commented Nov 17, 2020

closes #51

@alexhan01 alexhan01 linked an issue Nov 17, 2020 that may be closed by this pull request
@alexhan01
Copy link
Author

What it does: Populates the listOfInjectableURLRegex with regex-form of the Urls from the local news-sources.json file.

Copy link
Collaborator

@tarikeshaq tarikeshaq left a comment

Choose a reason for hiding this comment

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

Thanks @alexhan01!!! A couple of nits here and there but looks good to me! Tested it and it works!

// Parses allowed news sources from news-sources.json
function parseAllowedNewsSource() {
// Fetches data from local .json file
fetch('../static/news-sources.json')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it possible to preserve the promise here?

Since this runs asynchrounosly, it would be nice to .then it later to make sure it's complete before we actually do any validation (as the array is empty initially, and is only populated once the promise is resolved)

"^https:\\/\\/www\\.stackoverflow\\.com",
"^https:\\/\\/www\\.bbc\\.com\\/news\\/"
var listOfInjectableURLRegex = [ // change these to the list of allowed news sources
// "^https:\\/\\/www\\.google",
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can safely remove those comments

var regexedUrl = new RegExp(rawUrl);
listOfInjectableURLRegex.push(regexedUrl);
}
// console.log(listOfInjectableURLRegex); // For Testing
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto on removing the comments

function addToListOfInjectableURLRegex(json) {
const size = json.numRows;
for (i = 0; i < size; i++) {
var rawUrl = json.dataRows[i].baseUrl;
Copy link
Collaborator

@tarikeshaq tarikeshaq Nov 17, 2020

Choose a reason for hiding this comment

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

nit: Should be const not var

In modern javascript var is virtually never used. If you expect the variable to be re-assigned, you use let, otherwise you use const

const size = json.numRows;
for (i = 0; i < size; i++) {
var rawUrl = json.dataRows[i].baseUrl;
var regexedUrl = new RegExp(rawUrl);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto on const

// Helper function for parseAllowedNewsSource
function addToListOfInjectableURLRegex(json) {
const size = json.numRows;
for (i = 0; i < size; i++) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
for (i = 0; i < size; i++) {
for (let i = 0; i < size; i++) {

I would just use forEach though to avoid the indexing

@MayKikuchi
Copy link
Contributor

So recently I was thinking it would probably be simpler to use just the content.js file and not the background.js file, since content.js is simpler and we probably don't need any script running in the background, in which case, it might make the most sense to have the regex checking in popup.js or content.js depending on how/where we want to use it (and delete the background.js file)? What do you guys think @alexhan01 @tarikeshaq?

@alexhan01
Copy link
Author

@MayKikuchi for the regex checking part - we would just need to run it everytime the user opens a new tab right? And for populating the listOfInjectableURLRegex array, we just need to do it once - I'm unsure at which point though (do we populate the array when a new tab is open? when the extension is installed? etc.).

Regardless, for both cases above, I agree that we wouldn't need to constantly do regex-checking and array loading in the background.

@MayKikuchi
Copy link
Contributor

@MayKikuchi for the regex checking part - we would just need to run it everytime the user opens a new tab right?

I would think so @tarikeshaq

And for populating the listOfInjectableURLRegex array, we just need to do it once - I'm unsure at which point though (do we populate the array when a new tab is open? when the extension is installed? etc.).

I would think it might be easier to populate it each time the user opens the extension, otherwise if we only want to populate it once, we would need to store it somewhere (chrome storage?) and then check if the list exists in storage + fetch the list from storage anyways when we want to use it, wouldn't we?

@tarikeshaq
Copy link
Collaborator

Sorry I took my time to respond! And yes - I agree, we should run it once only for new tabs.

On how often to populate the array.... Honestly? if it makes it easier. Lets hard code them in the js files. They are kinda already hard coded in that json ( I originally wanted json to reflect what we might be receiving from the network, but that's overengineering).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Import the news-sources.json and add validation for them
3 participants