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

fix(background): Tweak logic to inject script programmatically #340

Merged
merged 3 commits into from
Jul 19, 2023

Conversation

ERosendo
Copy link
Contributor

@ERosendo ERosendo commented Jul 18, 2023

This PR fixes freelawproject/recap#346.

The current implementation of the extension inserts scripts listed in the CONTENT_SCRIPT_FILES array calling the executeScripts method sequentially inside a for-loop. I think this approach is causing the bug in Firefox because the executeScript is an asynchronous function, so there may be calls that are still running when the next one starts (which is not desired).

We need to make sure the extension injects a new script after the previous one succeeds and so on. Luckily we can pass a callback functionexecuteScripts method so we can nest the executeScript calls like this:

chrome.tabs.executeScript(null, { file: "jquery.js" }, function() {
      chrome.tabs.executeScript(null, { file: "master.js" }, function() {
          chrome.tabs.executeScript(null, { file: "helper.js" }, function() {
              chrome.tabs.executeScript(null, { file: "client.js" })
          })
      })
  })

The previous code makes sure the master.js script is injected after the jquery.js file and so on, but the nesting can get unwieldy so this PR adds a helper function to abstract the previous approach and handle the programmatically script injection.

@ERosendo ERosendo marked this pull request as ready for review July 18, 2023 15:08
@ERosendo ERosendo assigned mlissner and unassigned mlissner Jul 18, 2023
@ERosendo ERosendo requested a review from mlissner July 18, 2023 15:09
Copy link
Member

@mlissner mlissner left a comment

Choose a reason for hiding this comment

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

One suggestion about comments, but otherwise looks good to me.

src/background.js Outdated Show resolved Hide resolved
This commit adds a comment to for this method, formats the code using Prettier and replaces the for loop with a for...of statement
@ERosendo ERosendo requested a review from mlissner July 18, 2023 23:32
@mlissner mlissner dismissed their stale review July 19, 2023 00:25

It's all good

@mlissner mlissner removed their request for review July 19, 2023 00:25
@mlissner mlissner merged commit 8850385 into main Jul 19, 2023
@mlissner mlissner deleted the fix-contect-script-ordering-firefox branch July 19, 2023 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

The extension is not inserting banners in the submission form page
2 participants