如何在 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!');
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?有什么办法吗?上面的代码不起作用!
所以,我正在编写一些实现另一个函数的示例代码Constructor[Symbol.hasInstance],我注意到我的新实现不会被调用.
下面的脚本是我预期会发生的:
function Pirate(name) {
this.name = name;
}
const jackSparrow = {
isPirate: true
};
// Notice how `jackSparrow` is not yet considered an instance of the `Pirate` object
console.log(jackSparrow instanceof Pirate); // false
// Now let's assign another function for `Pirate[Symbol.hasInstance]`
Pirate[Symbol.hasInstance] = function (anObj) {
return anObj.isPirate;
};
// This will cause Pirate[Symbol.hasInstance] to be called with `jackSparrow`
console.log(jackSparrow instanceof Pirate); // true
Run Code Online (Sandbox Code Playgroud)
我尝试添加console.log对我的Pirate [Symbol.hasInstance]实现的调用,但它不会将任何内容记录到控制台.
有没有人知道发生了什么?为什么我的实现没有被调用?
我在Node 6.9.1上运行它.
我有这个脚本:
$("#test").click(function() {
alert('hue');
});
Run Code Online (Sandbox Code Playgroud)
我的html上有这个元素:
<li>
<a id="test" href="#contato">Contato</a>
</li>
Run Code Online (Sandbox Code Playgroud)
我已经导入了我的滚动脚本和jQuery.js脚本.
<script src="js/scrolling.js"></script>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
但是当我点击我的元素时,没有警报()!我究竟做错了什么?我怎样才能解决这个问题?