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

browser.extension null #120

Open
tidys opened this issue Apr 4, 2021 · 7 comments
Open

browser.extension null #120

tidys opened this issue Apr 4, 2021 · 7 comments

Comments

@tidys
Copy link

tidys commented Apr 4, 2021

Describe the bug

var browser = injectionContext.browser; // browser=null
var extension = browser.extension, // fuck null

To Reproduce

  1. 配置vue.config.js
{
	"pluginOptions": {
		"browserExtension": {
			"componentOptions": {
				"contentScripts": {
					"components": {
						background: true,
						contentScripts: true,
					},
					"entries": {
						content: "src/content.js",
						inject: "src/inject.js"
					}
				}
			}
		}
	}
}
  1. content.js中注入脚本inject.js
 let content = chrome.extension.getURL("js/inject.js")
  console.log(`[cc-inspector]注入脚本:${content}`);
  let script = document.createElement('script')
  script.setAttribute('type', 'text/javascript')
  script.setAttribute('src', content)
  script.onload = function () {
    // 注入脚本执行完后移除掉
    this.parentNode.removeChild(this);
  }
  document.body.appendChild(script)
  1. npm run build-watch
    package.json
"scripts":{
"build-watch": "vue-cli-service build --watch"
}
  1. 加载后,出现null问题,似乎是引用的其他插件导致的
    image

代码好像来自:

/* tslint:disable */

/* -------------------------------------------------- */

/* Start of Webpack Hot Extension Middleware */

/* ================================================== */

/* This will be converted into a lodash templ., any */

/* external argument must be provided using it */

/* -------------------------------------------------- */

Expected behavior
这个问题影响了我的开发体验,导致content无法注入的脚本无法正常工作

@tidys
Copy link
Author

tidys commented Apr 5, 2021

其实我的需求仅仅是生成一份inject.js,然后在conten.js中注入这个脚本
似乎我的用法有点问题,为了生成单独的inject.js,我不应该将将它配置在containScript.entrys,这种做法就会导致上述问题

但是我发现似乎没有其他更好的解决办法,插件似乎本身也没有考虑到脚本注入这方面的需求

希望能给出个解决办法,谢谢

@lemonly14
Copy link

老哥 我遇到了同样的问题

@tidys
Copy link
Author

tidys commented Nov 1, 2021

我暂时注释掉vue-cli-plugin-browser-extension代码中的

webpackConfig.plugin('extension-reloader').use(ExtensionReloader, [{ entries, ...extensionReloaderOptions }])

反正是使用,暂时规避了问题

@lemonly14
Copy link

这样的注释作用是什么?
image
可以这样写是吗

@tidys
Copy link
Author

tidys commented Nov 2, 2021

是手动修改这个插件的代码

@lemonly14
Copy link

老哥 可以加微信详聊吗 有些问题想请教一下 谢谢老哥

@tidys
Copy link
Author

tidys commented Nov 2, 2021

微信号:
xu__yanfeng

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants