Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

[MOC-198] find and replace #153

Merged
merged 5 commits into from
Aug 22, 2024
Merged

[MOC-198] find and replace #153

merged 5 commits into from
Aug 22, 2024

Conversation

fitzk
Copy link
Contributor

@fitzk fitzk commented Aug 22, 2024

Related PR: https://github.com/Mocksi/nest/pull/22

Screenshot 2024-08-22 at 12 17 42 PM

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features
    • Enhanced messaging structure in the Chrome extension with added context in the responses.
    • Introduced dynamic text replacement functionality within the Iframe component, triggered by specific messages.
  • Improvements
    • Improved modularity and maintainability of the code for better handling of text replacements.
  • Configuration
    • Established a robust Vite configuration for developing and building the Chrome extension, integrating necessary plugins and settings for both development and production environments.

Copy link

linear bot commented Aug 22, 2024

Copy link

coderabbitai bot commented Aug 22, 2024

Walkthrough

Walkthrough

The changes involve enhancements to the messaging handling in a Chrome extension's background script, the introduction of a new findReplaceAll function for text manipulation within an Iframe component, and the establishment of comprehensive Vite configurations for developing the extension. These modifications facilitate better communication between components, improve code maintainability, and streamline the build process for a React-based web extension.

Changes

File Change Summary
apps/mocksi-lite-next/src/pages/background/index.ts Updated message handling logic to include a new data property in responses for both onMessage and onMessageExternal listeners.
apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx Introduced a new asynchronous function findReplaceAll for dynamic text replacement, updating the control flow with a new NEW_EDIT message type.
apps/mocksi-lite-next/vite.config.ts.timestamp-1724351457304-9b25862a7b76b.mjs
apps/mocksi-lite-next/vite.config.ts.timestamp-1724351552357-a114ea3b305ca.mjs
Created Vite configuration files for the extension, integrating React and CRX plugins, and defining separate development and production manifests.

Sequence Diagram(s)

sequenceDiagram
    participant Background as Background Script
    participant Content as Content Script
    participant Iframe as Iframe Component

    Background->>Content: Send message with NEW_EDIT type
    Content->>Iframe: Receive message
    Iframe->>Iframe: Trigger findReplaceAll function
    Iframe->>Iframe: Replace text in document body
    Iframe->>Content: Log modifications
Loading

🐰 In a world of code so bright,
A rabbit hops with pure delight.
With messages clear and functions new,
It replaces text, like magic, woohoo!
Vite's tune plays, a smooth, swift flow,
Hopping along, watch the changes grow! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between aac7b2b and de754cb.

Files selected for processing (4)
  • apps/mocksi-lite-next/src/pages/background/index.ts (2 hunks)
  • apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx (2 hunks)
  • apps/mocksi-lite-next/vite.config.ts.timestamp-1724351457304-9b25862a7b76b.mjs (1 hunks)
  • apps/mocksi-lite-next/vite.config.ts.timestamp-1724351552357-a114ea3b305ca.mjs (1 hunks)
Additional comments not posted (12)
apps/mocksi-lite-next/src/pages/background/index.ts (2)

70-73: Verify the correctness of data property usage in external messages.

Ensure that the data property in the message sent to the active tab is correctly populated and used in the rest of the codebase.

Run the following script to verify the usage of the data property in external messages:


37-41: Verify the correctness of data property usage.

Ensure that the data property in the sendResponse function is correctly populated and used in the rest of the codebase.

Run the following script to verify the usage of the data property in the codebase:

Verification successful

Verified: Correct usage of data property in sendResponse.

The data property in the sendResponse function is correctly populated and used within the apps/mocksi-lite-next/src/pages/background/index.ts file. There are no other relevant usages or manipulations of this specific data property elsewhere in the codebase.

  • apps/mocksi-lite-next/src/pages/background/index.ts: Lines 37-41
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the `data` property in the codebase.

# Test: Search for the usage of the `data` property. Expect: Correct usage in related functions.
rg --type javascript --type typescript -A 5 $'data:'

Length of output: 86


Script:

#!/bin/bash
# Description: Verify the usage of the `data` property in the codebase.

# Test: Search for the usage of the `data` property. Expect: Correct usage in related functions.
rg --type typescript --type typescriptreact -A 5 $'data:'

Length of output: 91


Script:

#!/bin/bash
# Description: Verify the usage of the `data` property in the codebase.

# Test: Search for the usage of the `data` property. Expect: Correct usage in related functions.
rg --type ts --type tsx -A 5 $'data:'

Length of output: 64


Script:

#!/bin/bash
# Description: Verify the usage of the `data` property in the codebase.

# Test: Search for the usage of the `data` property. Expect: Correct usage in related functions.
rg --glob '*.ts' --glob '*.tsx' -A 5 $'data:'

Length of output: 6914

apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx (3)

Line range hint 91-125: Verify the appropriateness and consistency of inline styles.

Ensure that the inline styles set for the iframe based on different message types are appropriate and consistent.

Run the following script to verify the appropriateness and consistency of the inline styles:


37-61: Verify the correctness of findReplaceAll function parameters and logic.

Ensure that the parameters find, replace, and highlight are correctly used in the findReplaceAll function and that the logic for pushing modifications is accurate.

Run the following script to verify the usage of the findReplaceAll function parameters and logic:

Verification successful

The findReplaceAll function parameters and logic are correct.

The function is used consistently with its definition, and the logic for pushing modifications and handling highlights is appropriate.

  • The parameters find, replace, and highlightEdits are correctly used in the function call.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the `findReplaceAll` function parameters and logic.

# Test: Search for the usage of the `findReplaceAll` function. Expect: Correct usage in related functions.
rg --type javascript --type typescript -A 5 $'findReplaceAll'

Length of output: 95


Script:

#!/bin/bash
# Description: Verify the usage of the `findReplaceAll` function parameters and logic.

# Test: Search for the usage of the `findReplaceAll` function. Expect: Correct usage in related functions.
rg --type typescript --type tsx -A 5 'findReplaceAll'

Length of output: 88


Script:

#!/bin/bash
# Description: Verify the usage of the `findReplaceAll` function parameters and logic.

# Test: Search for the usage of the `findReplaceAll` function. Expect: Correct usage in related functions.
rg --type ts --type tsx -A 5 'findReplaceAll'

Length of output: 73


Script:

#!/bin/bash
# Description: Verify the usage of the `findReplaceAll` function parameters and logic.

# Test: Search for the usage of the `findReplaceAll` function. Expect: Correct usage in related functions.
rg -A 5 'findReplaceAll'

Length of output: 1160


63-81: Verify the handling of NEW_EDIT message type.

Ensure that the NEW_EDIT message type is correctly handled in the React.useEffect hook and that the findReplaceAll function is triggered accurately.

Run the following script to verify the handling of the NEW_EDIT message type:

Verification successful

Handling of NEW_EDIT message type is correctly implemented.

The NEW_EDIT message type is properly handled in the React.useEffect hook, where it logs the request and calls the findReplaceAll function with the necessary parameters.

  • Location: apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the handling of the `NEW_EDIT` message type.

# Test: Search for the handling of the `NEW_EDIT` message type. Expect: Correct handling in related functions.
rg --type javascript --type typescript -A 5 $'NEW_EDIT'

Length of output: 89


Script:

#!/bin/bash
# Description: Verify the handling of the `NEW_EDIT` message type.

# Test: Search for the handling of the `NEW_EDIT` message type. Expect: Correct handling in related functions.
rg --type ts --type tsx -A 5 $'NEW_EDIT'

Length of output: 67


Script:

#!/bin/bash
# Description: Verify the handling of the `NEW_EDIT` message type.

# Test: Search for the handling of the `NEW_EDIT` message type. Expect: Correct handling in related functions.
rg --type typescript --type typescriptreact -A 5 $'NEW_EDIT'

Length of output: 94


Script:

#!/bin/bash
# Description: Verify the handling of the `NEW_EDIT` message type.

# Test: Define file types and search for the handling of the `NEW_EDIT` message type.
rg --type-add 'ts:*.ts' --type-add 'tsx:*.tsx' --type ts --type tsx -A 5 $'NEW_EDIT'

Length of output: 747

apps/mocksi-lite-next/vite.config.ts.timestamp-1724351457304-9b25862a7b76b.mjs (3)

7-23: Verify the correctness and optimization of manifest configurations.

Ensure that the manifest configurations for development and production environments are correct and optimized.

Run the following script to verify the correctness and optimization of the manifest configurations:

Also applies to: 25-71


131-171: Verify the correctness and optimization of build settings.

Ensure that the build settings, including plugins, output directories, and aliases, are correct and optimized for the extension.

Run the following script to verify the correctness and optimization of the build settings:

Verification successful

Build settings are correctly configured and optimized.

The build settings in the Vite configuration file are correctly set up for both development and production environments, ensuring efficient builds and debugging capabilities.

  • emptyOutDir: Set to !isDev, ensuring the output directory is emptied only in production.
  • sourcemap: Enabled in development for easier debugging.
  • outDir: Properly configured for the output directory.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the correctness and optimization of build settings.

# Test: Search for the build settings. Expect: Correct and optimized settings.
rg --type javascript --type typescript -A 5 $'build'

Length of output: 86


Script:

#!/bin/bash
# Description: Verify the correctness and optimization of build settings in the specified Vite configuration file.

# Locate the Vite configuration file and search for build settings.
fd 'vite.config.ts.timestamp-1724351457304-9b25862a7b76b.mjs' --exec rg 'build' -A 5

Length of output: 332


2-5: Verify the correctness of import paths and versions.

Ensure that the import paths and versions for Vite plugins and utilities are correct and up-to-date.

Run the following script to verify the correctness of the import paths and versions:

apps/mocksi-lite-next/vite.config.ts.timestamp-1724351552357-a114ea3b305ca.mjs (4)

8-23: Verify security implications of content_security_policy.

The content_security_policy allows child-src and frame-src from http://localhost:3030, which is typical for development but should be reviewed for security implications in production.

Ensure that these settings are appropriate and secure for your development environment. Consider restricting these sources in production.


26-71: Verify security and privacy implications of content_security_policy and permissions.

The content_security_policy allows child-src and frame-src from http://nest-auth-ts-merge.onrender.com. The permissions list includes sensitive permissions like cookies, downloads, and webRequest.

Ensure that these settings are appropriate and secure for your production environment. Consider minimizing permissions and restricting sources in the content_security_policy.


75-129: LGTM! Package configuration aligns with project requirements.

The dependencies, devDependencies, and scripts are appropriate for a React-based web extension project.


131-171: LGTM! Vite configuration aligns with project requirements.

The build settings, plugins, and module resolution aliases are appropriate for a Chrome extension project using React.

Comment on lines 2 to 5
import { crx } from "file:///Users/kayla/Code/Mocksi/HARlighter/node_modules/.pnpm/@[email protected]/node_modules/@crxjs/vite-plugin/dist/index.mjs";
import react from "file:///Users/kayla/Code/Mocksi/HARlighter/node_modules/.pnpm/@[email protected][email protected]_@[email protected][email protected][email protected][email protected]_/node_modules/@vitejs/plugin-react/dist/index.mjs";
import { resolve } from "path";
import { defineConfig } from "file:///Users/kayla/Code/Mocksi/HARlighter/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/vite/dist/node/index.js";
Copy link

Choose a reason for hiding this comment

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

Consider using relative paths or package imports.

The use of absolute paths in imports is unconventional and may cause issues when the code is moved or shared.

Consider changing the imports to use relative paths or package imports:

-import { crx } from "file:///Users/kayla/Code/Mocksi/HARlighter/node_modules/.pnpm/@[email protected]/node_modules/@crxjs/vite-plugin/dist/index.mjs";
-import react from "file:///Users/kayla/Code/Mocksi/HARlighter/node_modules/.pnpm/@[email protected][email protected]_@[email protected][email protected][email protected][email protected]_/node_modules/@vitejs/plugin-react/dist/index.mjs";
-import { defineConfig } from "file:///Users/kayla/Code/Mocksi/HARlighter/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/vite/dist/node/index.js";
+import { crx } from "@crxjs/vite-plugin";
+import react from "@vitejs/plugin-react";
+import { defineConfig } from "vite";
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import { crx } from "file:///Users/kayla/Code/Mocksi/HARlighter/node_modules/.pnpm/@crxjs[email protected]/node_modules/@crxjs/vite-plugin/dist/index.mjs";
import react from "file:///Users/kayla/Code/Mocksi/HARlighter/node_modules/.pnpm/@vitejs[email protected][email protected]_@[email protected][email protected][email protected][email protected]_/node_modules/@vitejs/plugin-react/dist/index.mjs";
import { resolve } from "path";
import { defineConfig } from "file:///Users/kayla/Code/Mocksi/HARlighter/node_modules/.pnpm/vite@5.3.3_@[email protected][email protected][email protected][email protected]/node_modules/vite/dist/node/index.js";
import { crx } from "@crxjs/vite-plugin";
import react from "@vitejs/plugin-react";
import { resolve } from "path";
import { defineConfig } from "vite";

@fitzk fitzk requested review from elg0nz and jonathankap August 22, 2024 19:38
Copy link
Contributor

@jonathankap jonathankap left a comment

Choose a reason for hiding this comment

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

Please remove the two extra files

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is an internal file that shouldn't be checked in.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also remove from commit

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes for sure, just pushed up another PR that adds them to .gitinore too

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between de754cb and 15172f4.

Files selected for processing (1)
  • apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx (2 hunks)
Additional comments not posted (1)
apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx (1)

63-79: Verify the correct usage of sendResponse.

Ensure that sendResponse is called correctly within the asynchronous listener to prevent potential issues with message handling.

Run the following script to verify the usage of sendResponse:

Verification successful

Correct usage of sendResponse verified.

The sendResponse function is used correctly within the asynchronous listener in mocksi-extension.tsx, ensuring proper message handling by returning true to keep the message channel open.

  • File: apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify correct usage of sendResponse in onMessage listener.

# Test: Search for the usage of sendResponse. Expect: Proper usage within async listeners.
rg --type js --type ts -A 5 $'sendResponse'

Length of output: 6826

Comment on lines +37 to +60
async function findReplaceAll(
find: string,
replace: string,
highlight: boolean,
) {
const modification: ModificationRequest = {
description: `Change ${find} to ${replace}`,
modifications: [
{
action: "replaceAll",
content: `/${find}/${replace}/`,
selector: "body",
},
],
};

const modifications = await reactor.pushModification(modification);
if (highlight) {
for (const mod of modifications) {
mod.setHighlight(true);
}
});
}
console.log("mods in find and replace fn: ", modifications);
return modifications;
Copy link

Choose a reason for hiding this comment

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

Consider adding error handling for asynchronous operations.

The findReplaceAll function uses await for the reactor.pushModification call. Consider adding error handling to manage potential exceptions.

Apply this diff to add error handling:

async function findReplaceAll(
  find: string,
  replace: string,
  highlight: boolean,
) {
  try {
    const modification: ModificationRequest = {
      description: `Change ${find} to ${replace}`,
      modifications: [
        {
          action: "replaceAll",
          content: `/${find}/${replace}/`,
          selector: "body",
        },
      ],
    };

    const modifications = await reactor.pushModification(modification);
    if (highlight) {
      for (const mod of modifications) {
        mod.setHighlight(true);
      }
    }
    console.log("mods in find and replace fn: ", modifications);
    return modifications;
  } catch (error) {
    console.error("Error in findReplaceAll:", error);
    throw error;
  }
}
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async function findReplaceAll(
find: string,
replace: string,
highlight: boolean,
) {
const modification: ModificationRequest = {
description: `Change ${find} to ${replace}`,
modifications: [
{
action: "replaceAll",
content: `/${find}/${replace}/`,
selector: "body",
},
],
};
const modifications = await reactor.pushModification(modification);
if (highlight) {
for (const mod of modifications) {
mod.setHighlight(true);
}
});
}
console.log("mods in find and replace fn: ", modifications);
return modifications;
async function findReplaceAll(
find: string,
replace: string,
highlight: boolean,
) {
try {
const modification: ModificationRequest = {
description: `Change ${find} to ${replace}`,
modifications: [
{
action: "replaceAll",
content: `/${find}/${replace}/`,
selector: "body",
},
],
};
const modifications = await reactor.pushModification(modification);
if (highlight) {
for (const mod of modifications) {
mod.setHighlight(true);
}
}
console.log("mods in find and replace fn: ", modifications);
return modifications;
} catch (error) {
console.error("Error in findReplaceAll:", error);
throw error;
}

@fitzk fitzk requested a review from jonathankap August 22, 2024 21:21
Copy link
Contributor

@jonathankap jonathankap left a comment

Choose a reason for hiding this comment

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

Looks good

@fitzk fitzk merged commit 68b4db3 into main Aug 22, 2024
3 checks passed
@fitzk fitzk deleted the MOC-198_find-and-replace branch August 22, 2024 21:56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants