我是扩展开发的新手,也许有人已经为我的问题准备了一个小例子。
我计划了一个更复杂的代码(将执行 chromium API 函数),但是解决这个任务应该可以帮助我开始:
我想创建一个基于网站 javascript 调用触发弹出窗口或alert()(只是任何东西)的扩展。
例如,我的网站有一个按钮,当单击该按钮时,会执行带有一些参数的 JavaScript。
我的扩展程序会选取这些参数并根据这些参数执行 API(对于我的示例,只是任何弹出窗口)。
在我的基本示例中,我想使用网站 javascript 提供的文本触发某种弹出窗口/通知。
另外,只有我的网站域才应该被允许触发,其他任何内容都应该被拒绝。
我真的很感激帮助。
这是我的“空”清单
{
"name": "Special API",
"version": "1.0",
"description": "API demo extension.",
"browser_action":
{
"default_icon": "gears.ico",
"popup": "show_credits.htm"
},
"permissions": [
"http://www.mywebsite.com/"
]
}
这是我的 website.com/example 中的示例按钮
<html>
<body>
<button onClick="extension_do_exec('Hellow world','abcabc')">Execute extension function</button>
</body>
</html>
I'm new to extension development, maybe someone has a small example ready for my problem.
I plan a more complicate code (that will execute chromium API functions), but solving this task should help me get started:
I want to create an extension that triggers a popup or alert() (just anyything) based on a website javascript call.
So for example my website has a button, when clicked on the button a javascript with a few parameters is executed.
My extension picks those parameters up and executes APIs (for my example just any popup) based on the parameters.
In my basic example I'd like to trigger some sort of popup/notification with the text supplied by the website javascript.
Also only my website domain should be allowed to trigger that, anything else should be rejected.
I'd really appreciate help.
Here is my "empty" manifest
{
"name": "Special API",
"version": "1.0",
"description": "API demo extension.",
"browser_action":
{
"default_icon": "gears.ico",
"popup": "show_credits.htm"
},
"permissions": [
"http://www.mywebsite.com/"
]
}
Here the example button in my website.com/example
<html>
<body>
<button onClick="extension_do_exec('Hellow world','abcabc')">Execute extension function</button>
</body>
</html>
发布评论
评论(1)
您描述的方法是有问题的,因为网页的 javascript 和扩展程序的 javascript 是相互隔离的(存在 孤立的世界)。因此,不可能将“由网站 javascript 提供”的值直接获取到扩展程序的 javascript 中。我建议采用另一种方法。您可以通过将某些值作为属性分配给 DOM 对象来交换它们。可以从注入网页的内容脚本访问这些属性。当然,内容脚本可以确定页面的域并适当地工作。至于弹出窗口,这些是扩展程序的内部页面,您应该实现某种消息传递< /a> 它们和您的内容脚本之间。
The approach you described is problematic, bacause javascripts of web-pages, and javascripts of extensions are isolated from each other (there is a concept of isolated world). So it is not possible to get a value "supplied by the website javascript" directly into the extension's javascript. I'd suggest another approach. You possibly could exchange with some values by assigning them as properties to DOM objects. These properties can be accessed from a content script, injected into the web-page. Of course, the content script can determine domain of the page and work as appropriate. As for popups, these are internal pages of an extension, and you should implement some kind of messaging between them and your content script.