Ghostwriter is a JavaScript library that provides a simple API for programmatically interacting with input[type="text"]
elements. Give Ghostwriter the element you'd like to interact with along with a set of instructions that detail the interaction and Ghostwriter will take care of the rest. It's quite neat.
- Development (0.2.0) - ghostwriter.js
- Production (0.2.0) - ghostwriter.min.js ~1.7KB, compressed and gzipped
The available options that can be passed to ghostwriter#haunt
.
-
loop
- If truthy, when a haunt finishes, it will automatically start up again from the beginning. Defaults tofalse
. -
interval
- The number of milliseconds between the execution of strokes. Defaults to300
. -
input
- Theinput[type="text"]
element to haunt. Can be a CSS selector, DOM element, or a jQuery-wrapped DOM element. Required. -
manuscript
- The ordered collection of strokes that will be executed during a haunt. If you are using non-character strokes,manuscript
should be an array of strokes. If you are only using character strokes though, a string is acceptable. More details about strokes can be found in the strokes section. Required.
Returns a Haunt instance. options
is expected to be an options object (ahem, omnihash), an overview of the available options can be found in the options section.
var haunt = ghostwriter.haunt({
input: '.haunted-input'
, manuscript: 'i am haunted'
});
Starts/resumes the haunt. Can be paused by calling Haunt#pause
Pauses the haunt. Can be resumed by calling Haunt#start
.
Stops the haunt, resets the haunt, and blurs the input. The original value of the input will be restored.
Equivalent to calling Haunt#stop
and then Haunt#start
.
Restores the original value of the input. Only useful for cleanup after a haunt finishes.
During the life-cycle of a haunt, a number of jQuery events are triggered on the haunted input.
-
ghostwriter:start
- Triggered when the haunt starts. -
ghostwriter:resume
- Triggered when the haunt resumes after being paused. -
ghostwriter:pause
- Triggered when the haunt pauses. -
ghostwriter:stop
- Triggered when the haunt stops. -
ghostwriter:finish
- Triggered when the haunt finishes.
Haunts rely on manuscripts to determine how they should interact with inputs. Manuscripts are an ordered collection of strokes. There are many different types of strokes and each type has its own definition which details what should happen when it's executed. Stroke definitions are maintained in src/stroke_definitions.js.
Strokes that don't take any arguments don't need to be invoked before they are added to a manuscript. Along with the actions listed in their description, each stroke should trigger the expected DOM events when they are executed unless otherwise noted.
ghostwriter.character(char)
- If no text is selected, enterschar
into the input at the current cursor position. If text is selected, replaces the selected text withchar
.ghostwriter.backspace
- If no text is selected, moves the cursor one position backward and deletes the character at that position. If text is selected, removes the selected text.ghostwriter.left
- If no text is selected, moves the cursor one position left. If text is selected, moves the cursor to the left end of the selected text and deselects the text.ghostwriter.right
- If no text is selected, moves the cursor one position right. If text is selected, moves the cursor to the right end of the selected text and deselects the text.ghostwriter.up
ghostwriter.down
ghostwriter.enter
ghostwriter.tab
ghostwriter.esc
ghostwriter.selectLeft
- Extends selection one character to the left. Does not trigger any DOM events.ghostwriter.selectRight
- Extends selection one character to the right. Does not trigger any DOM events.ghostwriter.selectAll
- Selects all of the text in the input. Does not trigger any DOM events.ghostwriter.trigger(eventType)
- TriggerseventType
jQuery event on the input.ghostwriter.noop
- Does nothing.
Strokes have a convenience method attached to them to make repeating them dead simple. The following examples are equivalent.
var bad = ghostwriter.haunt({
input: '.i-am-an-input'
, manuscript: [
ghostwriter.character('z')
, ghostwriter.character('z')
, ghostwriter.character('z')
, ghostwriter.backspace
, ghostwriter.backspace
, ghostwriter.backspace
]
});
var good = ghostwriter.haunt({
input: '.i-am-an-input'
, manuscript: [
ghostwriter.character('z').repeat(3)
, ghostwriter.backspace.repeat(3)
]
});
If a string is found in a manuscript, Ghostwriter will split the string and turn each character into a character stroke. The following examples are equivalent.
var bad = ghostwriter.haunt({
input: '.i-am-an-input'
, manuscript: [
ghostwriter.character('c')
, ghostwriter.character('a')
, ghostwriter.character('s')
, ghostwriter.character('p')
, ghostwriter.character('e')
, ghostwriter.character('r')
]
});
var good = ghostwriter.haunt({
input: '.i-am-an-input'
, manuscript: ['casper']
});
var alsoGood = ghostwriter.haunt({
input: '.i-am-an-input'
, manuscript: 'casper'
});
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://thejakeharding.com/ghostwriter/releases/latest/ghostwriter.js"></script>
</head>
<body>
<input class="haunt-me" type="text">
<script>
var haunt = ghostwriter.haunt({
input: '.haunt-me'
, manuscript: [
'boo!'
, ghostwriter.backspace.repeat(4)
, 'sorry if i startled you'
, ghostwriter.selectAll
, ghostwriter.backspace
, 'i am actually quite friendly'
]
});
haunt.start();
</script>
</body>
</hmtl>
Tests are written using Jasmine. To run Ghostwriter's test suite with PhantomJS, run npm test
.
Found a bug? Create an issue on GitHub.
https://github.com/jharding/ghostwriter/issues
If you plan on contributing to Ghostwriter, be sure to read the contributing guidelines.
In order to build and test Ghostwriter, you'll need to install Ghostwriter's devDependencies ($ npm install
) and have grunt-cli installed. Below is an overview of Ghostwriter's Grunt tasks that'll be useful in development.
grunt build
- Builds ghostwriter.js from source files.grunt lint
- Runs source and test files through JSHint.grunt test
- Runs the test suite with PhantomJS.grunt test:browser
- Runs the test suite in your default browser.grunt watch
- Rebuilds ghostwriter.js whenever a source file is modified.
For transparency and insight into the release cycle, releases will be numbered with the follow format:
<major>.<minor>.<patch>
And constructed with the following guidelines:
- Breaking backwards compatibility bumps the major
- New additions without breaking backwards compatibility bumps the minor
- Bug fixes and misc changes bump the patch
For more information on semantic versioning, please visit http://semver.org/.
Copyright (c) 2013 Jake Harding
Licensed under the MIT License.