如何在 Greasemonkey 脚本中播放声音

How can I play a sound within a Greasemonkey script?

本文关键字:播放声音 脚本 Greasemonkey      更新时间:2023-09-26

如何在 Greasemonkey 脚本中播放声音?

我目前要做的是在达到条件时播放声音,例如:

// ==UserScript==
// @name Sound Alert
// @namespace example.com
// @include example.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @version 1
// @grant none
// ==/UserScript==
sound = new Audio("https://dl.dropbox.com/u/7079101/coin.mp3");
for (var i = 0; i <= 10; i++) {
  if (i === 10) {
    // Play a sound when i === 10
    sound.play();
  } else {
    console.log('Not yet!');
  }
}

我该怎么做?有什么办法吗?上面的代码不起作用!

好吧,似乎询问提高了找出问题正确答案的可能性(呵呵)。

这是我的解决方案:

// ==UserScript==
// @name    Sound Alert
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant   none
// ==/UserScript==
var player = document.createElement('audio');
player.src = 'https://dl.dropbox.com/u/7079101/coin.mp3';
player.preload = 'auto';
for (var i = 0; i <= 10; i++) {
  if (i === 10) {
    // Play a sound when i === 10
    player.play();
  } else {
    console.log('Not yet!');
  }
}

为了在 GreaseMonkey 脚本中播放声音,您必须遵循以下基本步骤:

第 1 步:创建类型为"音频"的元素

步骤 2:将源属性分配给文件位置。

var audio = document.createElement("audio");
audio.src = "https://dl.dropbox.com/u/7079101/coin.mp3";

这是一个解决方案

var audioformsg = new Audio();
audioformsg.src = 'http://www.podst.ru/pix/user_files/2/5564/Click_08.mp3';
audioformsg.autoplay = true;

从代码https://greasyfork.org/zh-CN/scripts/8149-sound-for-message/code