为什么ContentScriptFile不能在PageMod中运行?

时间:2022-08-26 14:19:29

I try to inject content script on page and use

我尝试在页面上注入内容脚本并使用

console.log("starting addon");
pageMod.PageMod({
    include: "*",//tempopary
    contentScriptFile: self.data.url("testPreload.js"),
    contentScriptWhen: 'start'});

testPreload.js:

testPreload.js:

console.log('testPreload');

I see "starting addon" in log and if I use contentScript:"console.log('testPreload')" instead of contentScriptFile I also see "testPreload".

我在日志中看到“启动插件”,如果我使用contentScript:“console.log('testPreload')”而不是contentScriptFile我也看到“testPreload”。

But when I use contentScriptFile I see "starting addon" but not "testPreload". What am I doing wrong?

但是当我使用contentScriptFile时,我看到“启动插件”而不是“testPreload”。我究竟做错了什么?

EDIT Error: Error opening input stream (invalid filename?) filePath resource://jid1-ktaxagdysynpew-at-jetpack/extension/data/testPreload.js

编辑错误:打开输入流时出错(文件名无效?)filePath资源://jid1-ktaxagdysynpew-at-jetpack/extension/data/testPreload.js

3 个解决方案

#1


4  

You want to move your testPreload.js file into the data directory. The self.data module is actually referencing that directory so the self.data.url() function gives you a valid URL to the files in that directory. FYI those URLs tend to look like resource://[your-jetpack-id]/data/[file])

您想将testPreload.js文件移动到数据目录中。 self.data模块实际上是引用该目录,因此self.data.url()函数为您提供了该目录中文件的有效URL。仅供参考这些网址看起来像资源:// [your-jetpack-id] / data / [file])

Again, just move your: lib/testPreload.js to data/testPreload.js and that should fix the problem.

再次,只需将您的:lib / testPreload.js移动到data / testPreload.js即可解决问题。

#2


2  

Your contentScript Files should reside in data directory to be able to access it through self.data.url('scriptname') .
Move your testPreload.js to data directory.

您的contentScript文件应驻留在数据目录中,以便能够通过self.data.url('scriptname')访问它。将testPreload.js移动到数据目录。

See https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts/Loading_Content_Scripts

请参阅https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts/Loading_Content_Scripts

#3


0  

Instead of:

代替:

contentScriptFile: self.data.url("testPreload.js"),

Use:

使用:

contentScriptFile: "resource://<your_extension_name>/testPreload.js",

self.Data.url() is a function that given a file path will return the full URI path to the file assuming it is under your add-on's data folder.

self.Data.url()是一个给定文件路径的函数,它将返回文件的完整URI路径,假设它位于加载项的数据文件夹下。

If you prefer to put your file at the root of the package, you can instead build the URI path yourself as in the above example. Just replace <your_extension_name> with the actual name you've given to your add-on in its package.json file.

如果您希望将文件放在包的根目录下,则可以自己构建URI路径,如上例所示。只需将 替换为您在package.json文件中为加载项提供的实际名称。

#1


4  

You want to move your testPreload.js file into the data directory. The self.data module is actually referencing that directory so the self.data.url() function gives you a valid URL to the files in that directory. FYI those URLs tend to look like resource://[your-jetpack-id]/data/[file])

您想将testPreload.js文件移动到数据目录中。 self.data模块实际上是引用该目录,因此self.data.url()函数为您提供了该目录中文件的有效URL。仅供参考这些网址看起来像资源:// [your-jetpack-id] / data / [file])

Again, just move your: lib/testPreload.js to data/testPreload.js and that should fix the problem.

再次,只需将您的:lib / testPreload.js移动到data / testPreload.js即可解决问题。

#2


2  

Your contentScript Files should reside in data directory to be able to access it through self.data.url('scriptname') .
Move your testPreload.js to data directory.

您的contentScript文件应驻留在数据目录中,以便能够通过self.data.url('scriptname')访问它。将testPreload.js移动到数据目录。

See https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts/Loading_Content_Scripts

请参阅https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts/Loading_Content_Scripts

#3


0  

Instead of:

代替:

contentScriptFile: self.data.url("testPreload.js"),

Use:

使用:

contentScriptFile: "resource://<your_extension_name>/testPreload.js",

self.Data.url() is a function that given a file path will return the full URI path to the file assuming it is under your add-on's data folder.

self.Data.url()是一个给定文件路径的函数,它将返回文件的完整URI路径,假设它位于加载项的数据文件夹下。

If you prefer to put your file at the root of the package, you can instead build the URI path yourself as in the above example. Just replace <your_extension_name> with the actual name you've given to your add-on in its package.json file.

如果您希望将文件放在包的根目录下,则可以自己构建URI路径,如上例所示。只需将 替换为您在package.json文件中为加载项提供的实际名称。