【问题标题】:Making an extension to notify when text field filled by website当网站填写文本字段时进行扩展以通知
【发布时间】:2019-07-29 13:43:17
【问题描述】:

我正在尝试进行扩展。它应该在网站每分钟自动刷新的文本字段中插入值时播放声音并显示通知。

对于通知,我发现这段代码可以正常工作,我把它放在 background.js 中:

createNotification();
audioNotification();

function audioNotification(){
var yourSound = new Audio('notification.mp3');
yourSound.play();
 }

function createNotification(){
var opt = {type: "basic",title: "Your Title",
          message: "Your message",iconUrl: "your_icon.png"}

chrome.notifications.create("notificationName",opt,function(){});

//include this line if you want to clear the notification after 5 seconds
setTimeout(function(){chrome.notifications.clear("notificationName",function(){});},5000);}

因此,如果已为文本字段分配了值,我需要执行通知代码。

我曾尝试在上面的代码之前放置一个 if 和 then 语句,但它似乎不起作用。我没有 content.js 文件,还是应该?

【问题讨论】:

  • 如果我没记错的话,谷歌浏览器不允许你在没有用户首先与网页上的按钮交互的情况下通过浏览器播放音频,这个权限将在每次页面加载时重置。
  • 我们建议它不是为了声音通知,我应该在 content.js 中使用什么命令来加载和执行上面的代码?
  • 我不知道content.js是什么。
  • 就像我上面提到的,我没有 content.js,因为我不知道如何使用它。我所知道的是我需要触发 background.js 并且它只能从 content.js 中发生?
  • 你在使用某种框架吗? content.js 可以是任何东西

标签: google-chrome notifications background


【解决方案1】:

我改变了方向,改用 Tampermonkey 中的脚本。

var textarea = document.getElementById('app_date');
var word = '2019-';
var textValue=textarea.value;
if (textValue.indexOf(word)!=-1)
{
var yourSound = new Audio('https://www.memoclic.com/medias/sons-wav/0/230.mp3');
yourSound.play();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多