如何将 Firefox 小书签转换为 Web 扩展或附加组件?
How to convert a Firefox bookmarklet to a web extension or add on?
我在 Firefox 中有以下简化的小书签,我正试图将其转换为网络扩展:
javascript:void%20function(){myobject.command(%22check%22,%22%22,this,%22%22)}();
当我点击小书签并且 myobject.command 得到 运行 时,它没有任何问题。 myobject 是在 my.website.com 上创建的,并提供了几个我可以通过 javascript.
运行 的命令
由于我想单击任务栏中的图标而不是书签中的 link,因此我尝试通过将此命令添加到简单内容来将其转换为 webextension/addon脚本 (content.js):
console.log("Hello");
myobject.command("check","",this,"");
console.log("Thanks for helping!");
我的 manifest.json 包含:
"content_scripts": [
{
"matches": ["*://my.website.com/*"],
"js": ["content.js"]
}]
这确保内容脚本仅在 my.website.com 上获得 运行。第一个 console.log 语句在 content.js 运行 时显示,但第二个不显示,因为脚本在 myobject.command 行失败。错误信息是 "myobject is not defined".
我做错了什么?为什么小书签有效但内容脚本无效?
这是一个范围问题,已使用以下答案解决:
- Insert code into the page context using a content script
我在 Firefox 中有以下简化的小书签,我正试图将其转换为网络扩展:
javascript:void%20function(){myobject.command(%22check%22,%22%22,this,%22%22)}();
当我点击小书签并且 myobject.command 得到 运行 时,它没有任何问题。 myobject 是在 my.website.com 上创建的,并提供了几个我可以通过 javascript.
运行 的命令由于我想单击任务栏中的图标而不是书签中的 link,因此我尝试通过将此命令添加到简单内容来将其转换为 webextension/addon脚本 (content.js):
console.log("Hello");
myobject.command("check","",this,"");
console.log("Thanks for helping!");
我的 manifest.json 包含:
"content_scripts": [
{
"matches": ["*://my.website.com/*"],
"js": ["content.js"]
}]
这确保内容脚本仅在 my.website.com 上获得 运行。第一个 console.log 语句在 content.js 运行 时显示,但第二个不显示,因为脚本在 myobject.command 行失败。错误信息是 "myobject is not defined".
我做错了什么?为什么小书签有效但内容脚本无效?
这是一个范围问题,已使用以下答案解决:
- Insert code into the page context using a content script