Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I play a sound within a Greasemonkey script?

How can I play a sound within a Greasemonkey script?

What I'm trying to do currently is to play a sound whenever a condition is reached, something like:

// ==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!');
  }
}

How can I do this? Is there any way to do so? The code above isn't working!

like image 759
lucasfcosta Avatar asked Jan 09 '15 13:01

lucasfcosta


3 Answers

Well, it seems like asking improves the possibility of finding out the correct answer for a problem (hehehe).

Here's my solution:

// ==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!');
  }
}
like image 122
lucasfcosta Avatar answered Sep 27 '22 17:09

lucasfcosta


Inorder to play a sound in GreaseMonkey script, you have to follow these basic steps:

Step 1: Create an element of type "Audio"

Step 2: Assign the source property to the file location.

var audio = document.createElement("audio");
audio.src = "https://dl.dropbox.com/u/7079101/coin.mp3";
like image 20
Santosh Panda Avatar answered Sep 27 '22 18:09

Santosh Panda


Here's a solution

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

from code https://greasyfork.org/zh-CN/scripts/8149-sound-for-message/code

like image 44
Dave B Avatar answered Sep 27 '22 19:09

Dave B