Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/bgsound.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Sounds on websites have rough history. While playing sounds can give more options to grab a users attention, using `<bgsound>` is generally seen as very **BAD** form.

There are various ways to play sounds using Javscript that use common standards which can be used rather.

The big issue with using `<bgsound>` is that users have no way to control when the sound starts and no way to stop it.

# How do I fix this ?

Playing audio can happen straight from plain javascript in most popular browsers:

```
var audio = new Audio('sound.mp3');
audio.play();
```

Frameworks such as [howler.js](http://goldfirestudios.com/blog/104/howler.js-Modern-Web-Audio-Javascript-Library) also exist that provides more features in multiple browsers.

# Resources

* [howler.js](http://goldfirestudios.com/blog/104/howler.js-Modern-Web-Audio-Javascript-Library)
* [HTMLAudioElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement)
* [Playing audio with Javascript](http://stackoverflow.com/questions/9419263/playing-audio-with-javascript)
* [Is using bgsound a bad idea](https://www.thecodingforums.com/threads/is-using-bgsound-a-bad-idea.687818/)
57 changes: 57 additions & 0 deletions lib/rules/bgsound.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// modules
var S = require('string');
var _ = require('underscore');
var cheerio = require('cheerio');

// actual hashes that check the content
var swfCodePattern = new RegExp(/"(.*?).swf"|'(.*?).swf'/gi);

// expose the actual function
module.exports = exports = function(payload, fn){

// get the data
var data = payload.getData();

// get the page content to start using it
payload.getPageContent(function(err, content) {

// check for errors, else stop
if(err) return fn(err);

// check if content was given
if(S(content || '').isEmpty() == true) {

// done
return fn(null);

}

// load in our cheerio context
$ = cheerio.load(content);

// get all the object tags
$('bgsound').each(function(i, elem) {

// add the rule
payload.addRule({

type: 'error',
message: 'Do not use the <bgsound> element',
key: 'bgsound'

}, {

message: '$ is very much looked down on',
type: 'text',
identifiers: [ 'bgsound' ]

});

});

// add all our rules
fn(null);

});

};
3 changes: 2 additions & 1 deletion lib/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module.exports = exports = [

require('./w3c'),
require('./plugins/flash')
require('./plugins/flash'),
require('./bgsound')

];
5 changes: 5 additions & 0 deletions samples/bgsound.missing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<p>No annoying sound in the background ...</p>
</body>
</html>
6 changes: 6 additions & 0 deletions samples/bgsound.present.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<p>Some annoying sound in the background ...</p>
<bgsound />
</body>
</html>
76 changes: 76 additions & 0 deletions test/bgsound.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// modules
var assert = require('assert')
var _ = require('underscore')
var fs = require('fs');
var passmarked = require('passmarked');
var pluginFunc = require('../lib/rules/plugins/applets');

describe('bgsound', function(){

// handle the settings
it('Should not return a error error if <bgsound /> is present', function(done){

// create a dummy payload
payload = passmarked.createPayload(
{

url: 'http://example.com'

},
{},
fs.readFileSync('./samples/bgsound.missing.html').toString());

// handle the stream
pluginFunc(payload, function(err){

// check for a error
if(err) assert.fail(err);

// get the rules
var rules = payload.getRules();

// check if we got any rules back ...
if(rules.length > 0)
assert.fail('Was not expecting a error');

// done
done();

});

});

// handle the settings
it('Should not return a error error if <bgsound /> is missing', function(done){

// create a dummy payload
payload = passmarked.createPayload(
{

url: 'http://example.com'

},
{},
fs.readFileSync('./samples/bgsound.missing.html').toString());

// handle the stream
pluginFunc(payload, function(err){

// check for a error
if(err) assert.fail(err);

// get the rules
var rules = payload.getRules();

// check if we got any rules back ...
if(rules.length > 0)
assert.fail('Was expecting an error');

// done
done();

});

});

});