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: CRXJS does not work with content script in "MAIN" world, link is… #851

Closed
wants to merge 4 commits into from

Conversation

mLiGuangYuan
Copy link

…sues: #695

Copy link

changeset-bot bot commented Jan 2, 2024

⚠️ No Changeset found

Latest commit: 5744c1b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Jan 2, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
vite-plugin-docs ⬜️ Ignored (Inspect) Visit Preview Jan 14, 2024 7:11pm

@MindsightsAI
Copy link

Is it fully solve #695 ?

@mLiGuangYuan
Copy link
Author

mLiGuangYuan commented Jan 5, 2024

Is it fully solve #695 ?

yep, but this is just an idea to solve the problem, just for reference, this PR has been linked to #695, I feel that the PR I submitted is just a temporary solution, I hope you help to improve it!

@mLiGuangYuan
Copy link
Author

I realize that this form of code I'm submitting is up for consideration, and I look forward to your better solutions!

@MindsightsAI
Copy link

MindsightsAI commented Jan 5, 2024

@mLiGuangYuan is it work with hot reload? Do you have workable solution with additional content script in MAIN world that work with hot reload?

@mLiGuangYuan
Copy link
Author

mLiGuangYuan commented Jan 5, 2024

@mLiGuangYuan is it work with hot reload? Do you have workable solution with additional content script in MAIN world that work with hot reload?

Yes, It's does support hot reloading, just remove the [hash].filename from vite rollup config, like

...others
build: {
    /** Can slowDown build speed. */
    // sourcemap: isDev,
    outDir,
    minify: isProduction,
    reportCompressedSize: isProduction,
    rollupOptions: {
      output: {
        format: 'esm',
        entryFileNames: 'entry/[name].js',
        chunkFileNames: 'chunks/[name].js',
        assetFileNames: 'src/[name].[ext]',
      },
    },
  },

@mLiGuangYuan
Copy link
Author

cxtjs-vite-plugin.zip

@mLiGuangYuan is it work with hot reload? Do you have workable solution with additional content script in MAIN world that work with hot reload?

you can try it, in your project, but u need be careful with the variable I set in localStorage, __CRX_EXTEND_ID__, it may have a conflict, don't override it!

@MindsightsAI
Copy link

MindsightsAI commented Jan 5, 2024

@mLiGuangYuan when i am trying to use your zip, all looks fine, but on dev or build process i have that error:

ahwe@user:~/mst-extension$ pnpm run build

> inside-me@ build /home/ahwe/mst-extension
> tsc && vite build

failed to load config from /home/ahwe/mst-extension/vite.config.ts
error during build:
TypeError: (0 , import_cxtjs_vite_plugin2.crx) is not a function
    at config (file:///home/ahwe/mst-extension/vite.config.ts.timestamp-1704474950853-286e3dfc02da4.mjs:110:39)
    at loadConfigFromFile (file:///home/ahwe/mst-extension/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-68d1a114.js:66181:15)
    at async resolveConfig (file:///home/ahwe/mst-extension/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-68d1a114.js:65774:28)
    at async build (file:///home/ahwe/mst-extension/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-68d1a114.js:47926:20)
    at async CAC.<anonymous> (file:///home/ahwe/mst-extension/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/cli.js:842:9)
 ELIFECYCLE  Command failed with exit code 1.
ahwe@user:~/mst-extension$ 

UPD: Same problem after build from source

@mLiGuangYuan
Copy link
Author

mLiGuangYuan commented Jan 6, 2024

@mLiGuangYuan when i am trying to use your zip, all looks fine, but on dev or build process i have that error:

ahwe@user:~/mst-extension$ pnpm run build

> inside-me@ build /home/ahwe/mst-extension
> tsc && vite build

failed to load config from /home/ahwe/mst-extension/vite.config.ts
error during build:
TypeError: (0 , import_cxtjs_vite_plugin2.crx) is not a function
    at config (file:///home/ahwe/mst-extension/vite.config.ts.timestamp-1704474950853-286e3dfc02da4.mjs:110:39)
    at loadConfigFromFile (file:///home/ahwe/mst-extension/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-68d1a114.js:66181:15)
    at async resolveConfig (file:///home/ahwe/mst-extension/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-68d1a114.js:65774:28)
    at async build (file:///home/ahwe/mst-extension/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-68d1a114.js:47926:20)
    at async CAC.<anonymous> (file:///home/ahwe/mst-extension/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/cli.js:842:9)
 ELIFECYCLE  Command failed with exit code 1.
ahwe@user:~/mst-extension$ 

UPD: Same problem after build from source

this is a package, need import 2 package..

how to use

image

version

image

@mLiGuangYuan
Copy link
Author

@mLiGuangYuan when i am trying to use your zip, all looks fine, but on dev or build process i have that error:

ahwe@user:~/mst-extension$ pnpm run build

> inside-me@ build /home/ahwe/mst-extension
> tsc && vite build

failed to load config from /home/ahwe/mst-extension/vite.config.ts
error during build:
TypeError: (0 , import_cxtjs_vite_plugin2.crx) is not a function
    at config (file:///home/ahwe/mst-extension/vite.config.ts.timestamp-1704474950853-286e3dfc02da4.mjs:110:39)
    at loadConfigFromFile (file:///home/ahwe/mst-extension/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-68d1a114.js:66181:15)
    at async resolveConfig (file:///home/ahwe/mst-extension/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-68d1a114.js:65774:28)
    at async build (file:///home/ahwe/mst-extension/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-68d1a114.js:47926:20)
    at async CAC.<anonymous> (file:///home/ahwe/mst-extension/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/cli.js:842:9)
 ELIFECYCLE  Command failed with exit code 1.
ahwe@user:~/mst-extension$ 

UPD: Same problem after build from source

if u don't konw how to work, u can use this npm https://www.npmjs.com/package/@belloai/cxtjs-vite-plugin

@jacksteamdev jacksteamdev self-assigned this Jan 7, 2024
@jacksteamdev jacksteamdev added the vite-plugin Related to Vite plugin label Jan 14, 2024
@jacksteamdev jacksteamdev added the content-script-world Related to main or isolated content script execution context label Jan 14, 2024
@depressedX
Copy link

多个插件在同一个页面上 storage key 相同,会被覆盖吧

@mLiGuangYuan
Copy link
Author

多个插件在同一个页面上 storage key 相同,会被覆盖吧

I realize that this form of code I'm submitting is up for consideration, and I look forward to your better solutions! can u help to fix it?

@@ -1,4 +1,6 @@
declare const __SCRIPT__: string

localStorage.setItem('__CRX_EXTEND_ID__', chrome.runtime.id)
Copy link

Choose a reason for hiding this comment

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

did you try to obtain it using the method described here by the chromium team? It would be cool to not rely on localStorage

// your injected.js. Only works as a first line in the injected script
extensionID = document.currentScript.id;

If not then lets try that

for (const {
js = [],
matches = [],
world = 'ISOLATED',
Copy link

@Toumash Toumash Jun 12, 2024

Choose a reason for hiding this comment

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

dont we need to pass the world argument into the first if leg? if (config.command === 'serve') {.

I guess it now works in the vite build mode, but does not work in vite (dev) mode

fileName: string
world?: 'ISOLATED' | 'MAIN'
}): string {
const { fileName, world = 'ISOLATED' } = params || {}
Copy link

Choose a reason for hiding this comment

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

i dont get it - this function will never get parameter { world: 'MAIN'}. The only usage this code does not include world parameter

@Toumash
Copy link

Toumash commented Jun 12, 2024

I would love to test it + help but im not sure how to setup the pnpm workspace (never worked with pnpm and workspaces)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
content-script-world Related to main or isolated content script execution context vite-plugin Related to Vite plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants