Google Chrome 扩展:在 webkit 通知中使用 Javascript?

发布于 2024-09-02 22:52:56 字数 1428 浏览 3 评论 0原文

我一直在遵循 Google Chrome 扩展程序开发人员指南,并且到目前为止创建一个具有浏览器操作的扩展程序,且不带会触发 webkit 通知的弹出窗口HTML 格式。

事实上,这个 HTML 文件包含操作按钮、链接和 javascript,但它们似乎都没有响应。

这是一种设计行为还是有办法在这些 webkit 通知中嵌入 javascript/链接/按钮?

编辑:

以下是关于该主题的一些新见解:

Javascript

这是一个不起作用的 HTML 通知页面:

<html>
<head>
<script>
  alert("hey");
</script>
</head>
<body>
  content
</body>
</html>

因此,通知全是空白;不显示文本“内容”

但如果我删除“警报”,则会显示文本“内容”。

我首先认为 Javascript 阻止了页面正确渲染,但我尝试了其他方法:

<script>document.write("content");</script>

此 javascript 命令正确执行并显示文本“content”。然后,我们可以假设即使在 webkit 桌面通知中也启用了 Javascript,但“警报”功能被禁用,并且破坏了通知页面的渲染。

链接

有些链接有效,有些无效。以下是我迄今为止测试过的内容的快速列表:

<a href="http://www.google.com/">Link</a>                   # => Doesn't work
<a href="http://www.google.com/" target="_top">Link</a>     # => Doesn't work
<a href="http://www.google.com/" target="_parent">Link</a>  # => Doesn't work
<a href="http://www.google.com/" target="_blank">Link</a>   # => Works (new tab)

I have been following the Google Chrome Extensions Developer's guide and have been able so far to create an extension with a browser action without popup that would trigger a webkit notification in HTML format.

Fact is that this HTML file contains action buttons, links and javascript, but none of them seems to respond.

Is this a by-design behavior or is there a way for embedding javascript/links/buttons in these webkit notifications?

EDIT:

Here are some new insights on the subject:

Javascript

Here is an HTML notification page that doesn't work:

<html>
<head>
<script>
  alert("hey");
</script>
</head>
<body>
  content
</body>
</html>

As a result, the notification is all blank ; the text "content" isn't displayed.

But if I remove the "alert", the text "content" is displayed.

I first thought that Javascript was blocking the page from rendering correctly, but I tried something else:

<script>document.write("content");</script>

This javascript command is correctly executed and displays the text "content". We can then assume that Javascript is enabled even in the webkit desktop notifications, but the "alert" function is disabled, and breaks the rendering of the notification page.

Links

Some links work, some don't. Here is a quick list of those I've tested so far:

<a href="http://www.google.com/">Link</a>                   # => Doesn't work
<a href="http://www.google.com/" target="_top">Link</a>     # => Doesn't work
<a href="http://www.google.com/" target="_parent">Link</a>  # => Doesn't work
<a href="http://www.google.com/" target="_blank">Link</a>   # => Works (new tab)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光是把杀猪刀 2024-09-09 22:52:56

Chromium 桌面通知 API 规范 中对此进行了全部解释其中明确指出:

如果用户代理实现 createHTMLNotification,它应该将 HTML 通知显示为独立的浏览上下文,其功能与任何其他 HTML 网页相同,但具有以下属性:

  • 无法导航通知。通知上下文中窗口对象的 Location 属性应该是只读的。所有链接都应在新的无通知浏览上下文中打开。
  • 如果在通知上下文中的窗口对象上调用 close() 以响应用户手势,则用户代理应像用户关闭通知一样继续操作,包括所有必需的事件处理。

It's all explained in the Chromium Desktop Notifications API Specification where it's clearly said that:

If a user agent implements createHTMLNotification, it should show HTML notifications as independent browsing contexts which are equivalent in functionality to any other HTML web page, except with the following properties:

  • The notification cannot be navigated. The Location attribute of the window object in the notification context should be readonly. All links should open in new, non-notification browsing contexts.
  • If close() is invoked on the window object in the notification context in response to a user gesture, the user agent should proceed as if the user closed the notification, including all required event processing.
隱形的亼 2024-09-09 22:52:56

更新:createHTMLNotification()从草稿中删除规范以及 Chrome,因此这将不再起作用。

最接近的是整个通知窗口上的 onclick 处理程序。它不太容易被发现(看起来不像链接),而且我还没有测试是否可以从它导航到链接。

UPDATE: createHTMLNotification() has been removed from the draft spec and also from Chrome, so this will no longer work.

The closest thing is an onclick handler on the whole notification window. It's less discoverable (doesn't look like a link), and I haven't tested if it's possible to navigate to a link from it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文