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(manifest): Upgrade to manifest v3 #387

Merged
merged 78 commits into from
Aug 28, 2024
Merged

Conversation

ERosendo
Copy link
Contributor

@ERosendo ERosendo commented Aug 5, 2024

This PR upgrades the extension code from Manifest V2 to Manifest V3, the newest version of the Chrome Extensions platform.

Here's a list of the key changes in this PR:

  • Updates in the manifest.js file:

    • Change the value of the "manifest_version" field from 2 to 3.

    • Host permissions in Manifest V3 are a separate field. This PR separates the old list into two list permissions and host_permissions

    • Manifest V3 limits exposure by restricting which websites and extensions can access resources( images and fonts) in the extension. Instead of providing a list of files as before, We provide an array of objects, each of which maps a set of resources to a set of URLs or extension IDs.

    • Manifest V3 for Chrome has deprecated background pages and scripts in favor of service workers. While Firefox continues to support background pages, Safari offers compatibility with both. To ensure cross-browser functionality, this PR introduces the service_worker key within the background object while preserving scripts key for broader support.

    • In the earlier versions, Actions used to be divided into browser_action and page_action. In Manifest V3, they are unified into a single field named action.

    • Due to inconsistencies in how Chrome and Firefox handle the favicon_url key within the search_provider object, this PR temporarily removes this object from the manifest to maintain cross-browser compatibility. A dedicated PR will address the favicon_url issue.

  • Migrate background script to a single service worker:

    • Manifest V3 introduces support for ES modules and the import keyword in service worker scripts, enabling improved code modularity and organization. This PR refactors several helper methods from the service worker into a new file, background.js, located within the utils folder. By isolating helper logic, the service worker is streamlined to focus exclusively on event handling, enhancing code readability and maintainability.

    • This PR also addresses Using exportInstance and importInstance goes against convention and causes issues recap#237 by refactoring code related to CL API data fetching and browser notifications. We've also removed all instances of the old code and associated helper functions. To improve code organization and efficiency, we've introduced the following changes:

      • Dedicated files: Created background_fetch.js to handle CL API data requests and background_notifier.js for managing browser notifications.
      • Wrapper functions: Implemented dispatchBackgroundFetch in fetch.js and dispatchBackgroundNotifier in notifier.js to serve as entry points for triggering background tasks.
    • Due to the incompatibility of XMLHttpRequest() with service workers, this PR replaces all instances of XMLHttpRequest() with the fetch() API. This ensures the code functions correctly within the service worker environment.

  • General code Updates:

    • Manifest V3 introduces the scripting API, replacing the tabs API's executeScript function for script injection. To accommodate this change, this PR updates the manifest file's permissions to include 'scripting'. This is necessary as we've adopted programmatic script injection as outlined in fix(background): Tweak logic to inject script programmatically #340. This change will trigger the permission window when updating the extension.

    • Upgrade to Chrome Runtime API. This PR modernizes the extension codebase by replacing deprecated chrome.extension APIs with their supported equivalents in the chrome.runtime API.

    • Migrating to Promises for Asynchronous Operations. This PR significantly enhances code readability and maintainability by replacing traditional callback functions with modern promise-based asynchronous programming. By leveraging async and await keywords, we've streamlined the process of fetching CL data and triggering notifications. This refactoring aligns with the broader goal of modernizing the RECAP codebase as outlined in Modernize RECAP codebase recap#370.

    • Due to enhanced extension security measures in Manifest V3, injecting custom script tags from content scripts is no longer allowed. Previously, this technique was used to tweak form submission behavior and extract data from ACMS. To address these limitations, this PR implements a new approach.

      Content scripts live in an isolated environment, preventing direct access to some data on the webpage. Consequently, we've used the scripting API and executeScript() method from the service worker to execute custom scripts within the desired context.

      To accommodate this change, the storeVueDataInSession and overwriteFormSubmitMethod functions have been refactored into wrappers that send messages to the service worker. The core logic has been moved to background script functions named getAndStoreVueData and overwriteSubmitMethod, respectively. This restructuring enables the execution of custom scripts in a secure and compliant manner.

    • Methods for handling district court, appellate, and ACMS reports have been refactored to utilize a new helper method that returns Promises.

    • Updates the karma.conf.js configuration file to align with the project's structural changes. Additionally, necessary mock data has been created to ensure test cases pass successfully

    • This PR fixes code formatting by aligning Prettier's printWidth setting with ESLint's 80-character limit. Additionally, it enhances code readability by standardizing indentation rules for switch cases and multi-line ternary expressions.

@ERosendo ERosendo added the no changelog Override Check Changelog Action. label Aug 7, 2024
Adds a "type" field with value "module" to the manifest file, enabling the use of the import statement for the background script.
Declares the "scripting" permission in the manifest file to enable script injection capabilities.
Improves code readability and maintainability by extracting reusable logic into dedicated helper functions.
@ERosendo ERosendo force-pushed the feat-upgrade-to-manifest-v3 branch from cc55da7 to a6195b7 Compare August 7, 2024 22:01
… interactions

This commit refactors RECAP API interactions into a dedicated helper function, leveraging background workers for improved performance and reliability.
Added a waitUntil helper function to prevent premature termination of the service worker during long-running operations like fetch requests or complex calculations.
Copy link
Contributor

@albertisfu albertisfu left a comment

Choose a reason for hiding this comment

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

Thanks! I reviewed the changes, and they worked correctly.
Just one suggestion to include the documentation URL for the trick to keep the worker alive.

@ERosendo ERosendo removed the no changelog Override Check Changelog Action. label Aug 28, 2024
@mlissner mlissner merged commit 3ebcd1d into main Aug 28, 2024
8 checks passed
@mlissner mlissner deleted the feat-upgrade-to-manifest-v3 branch August 28, 2024 03:12
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.

3 participants