-
-
Notifications
You must be signed in to change notification settings - Fork 386
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
inject custom content script from Popup.tsx #329
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution. We will check and reply to you as soon as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is good idea to create duplicated content script.
I mean create step by step instruction, where you describe how to use this, dev which want to implement this, can copy content script, describe only how to attach this to project, like you create in this PR, and describe how to use this excecuteScript :)
refreshOnUpdate('pages/content'); | ||
|
||
const root = document.createElement('div'); | ||
root.id = 'chrome-extension-boilerplate-react-vite-content-view-root'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
root.id = 'chrome-extension-boilerplate-react-vite-content-view-root'; | |
root.id = 'chrome-extension-boilerplate-react-vite-injected-content-view-root'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lealahm Why did you don't commit this suggestion?
May you relate this pull request to its issue #306 , please ? Thanks, |
Co-authored-by: Romain Dequidt <[email protected]>
@romaindequidt, Hello, I was also facing the same problem once I tested this out and still trying to solve this problem |
Co-authored-by: Romain Dequidt <[email protected]>
Co-authored-by: Romain Dequidt <[email protected]>
Co-authored-by: Romain Dequidt <[email protected]>
Co-authored-by: Romain Dequidt <[email protected]>
Co-authored-by: Romain Dequidt <[email protected]>
@lealahm any moves forward? |
@lealahm @romaindequidt the latest vite preload plugin already solved this problem, 685da06 |
I don't think these changes are enough; I was doing something very similar but I also needed to change this line: chrome-extension-boilerplate-react-vite/utils/plugins/inline-vite-preload-script.ts Line 10 in 685da06
In the case of this PR, it would need to be: if (!/content|injectedContent/.test(chunk.fileName)) { |
const injectContentScript = async () => { | ||
const [tab] = await chrome.tabs.query({ currentWindow: true, active: true }); | ||
console.log(tab.id) | ||
chrome.scripting.executeScript({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This results in
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'executeScript')
I think you need to have the scripting
permission in the manifest. You also need to have the activeTab
permission, or have the content script allowed as described here:
https://developer.chrome.com/docs/extensions/reference/api/scripting
I'm facing the same problem, it's explained in this issue, this PR will solve the problem? I want to preserve the behaviour of inject content automatically without open Popup manually. All code works fine when build in devleopment mode, but in production mode I got the mentioned error. |
@tngflx It didn't solve it for me; please can you share a working example? |
@aspiers Hi man, can I know what's your error? By running the inline script by default in vite.config.ts that should do it, that plugin is just a hook event inside vite upon bundling. Edit : ok I take back my word, the inline script on latest commit is rather buggy, the first line if (!/content/.test(chunk.fileName)) {
return null;
} already f us over... |
I already provided this solution in this comment. |
try my solution... #357 const filter = createFilter(options.include, options.exclude);
async writeBundle(outputOptions, bundle) {
const outputDir = outputOptions.dir || (outputOptions.file && path.dirname(outputOptions.file));
let fileToDelete: string | null = null;
if (!outputDir) {
console.error('Unable to determine the output directory.');
return;
}
for (const [fileName, outputChunk ] of Object.entries(bundle) as [string, OutputChunk][]) {
if (!filter(fileName)) {
continue; // Skip files that don't match the filter
}
let modifiedCode = outputChunk.code;
// Check if the filename contains "index.js"
if (fileName.includes('index.js')) {
// Replace import statements with the actual code
const importStatementRegex = /import\s*\{[^\}]*__vitePreload[^\}]*\}\s*from\s*['"]([^'"]+)['"];/g;
const exportStatementRegex = /export\s*\{\s*__vitePreload\s*as\s*_\s*\};/g;
modifiedCode = modifiedCode.replace(importStatementRegex, (_, relativePath) => {
const absolutePath = path.resolve(outputDir, path.dirname(fileName), relativePath);
try {
let fileContent = fs.readFileSync(absolutePath, 'utf-8');
fileContent = fileContent.replace(exportStatementRegex, '');
return fileContent;
} catch (error) {
console.error(`Error reading file: ${absolutePath}`, error);
return _; // Return the original import statement if there is an error
}finally {
// Store the file path for deletion outside the loop
fileToDelete = absolutePath;
}
})
}
try {
fs.writeFileSync(path.resolve(outputDir, fileName), modifiedCode, 'utf-8');
} catch (error) {
console.error(`Error writing file: ${path.resolve(outputDir, fileName)}`, error);
}
}
if (fileToDelete) {
try {
fs.unlinkSync(fileToDelete);
} catch (error) {
console.error(`Error removing file: ${fileToDelete}`, error);
}
}
}, I made this solution before i found the latest commit solution, mine works a bit better and skipped the filename check and only focus on index.js. Its a bit bloated maybe you can rewrite it with renderchunk hook |
To be honest, I simply started this boilerplate without full knowledge of Vite and the bundling process of Rollup that it uses internally, and I think this is the time to confront that limitation to some extent. I'm using a lot of vitePlugins to get things to do what I want, or to fix bugs, and I'm increasingly convinced that a fundamental solution is needed. And that is to completely tear down this boilerplate and fix it at a lower layer based on rollup. In the process, I think we can also implement a true HMR (not the inconvenient way we have now, where we throw in a refresh method and a path). The only limitation is my physical time... |
@Jonghakseo Makes sense, and I totally understand about the lack of time! So grateful for what you already built here so far though; it enabled me to build my extension which probably would have taken too long for me to do by myself! |
@Jonghakseo Are you want to implement this PR? |
@Jonghakseo One more ping :) |
Thanks~! I'll check it soon ^^ |
If you decide to move forward with that, maybe i can help with that, it have some months, i need to read it 1 more time :) |
@Jonghakseo Let's do something with this thread cause maybe somebody waiting for this feature :) |
I need this feature 😜 Would be fantastic if a solution was found 🙏 |
If I understand correctly, this should be worked on top of the legacy branch, is that correct? |
Maybe.... this is solution...? |
I think so |
Co-authored-by: Darwin Swartz <[email protected]> Co-authored-by: Leal <[email protected]> Co-authored-by: Romain Dequidt <[email protected]> Co-authored-by: Jonghakseo <[email protected]> Co-authored-by: PatrykKuniczak <[email protected]>
In the above code, I try adding a custom content script folder apart from the default
content
folder, which would be the code that can be injected on the page when the user clicks on a button inside popup.tsx or using background script.