phantom-jquery
Access DOM elements using jQuery in PhantomJS.
This project could be useful in scraping websites easily or in unit testing.
☁️ Installation
# Using npm npm install --save phantom-jquery # Using yarn yarn add phantom-jquery
📋 Example
// Dependenciesvar phJQuery = ; // Open my websitephJQuery; // => Index: 0 [Blog](/)// => Index: 1 [About](/about)// => Index: 2 [FAQ](/faq)// => Index: 3 [Contact](/contact)// => Index: 4 [How to convert JSON to Markdown using json2md](/blog/27-how-to-convert-json-to-markdown-using-json2md)// ...// => Index: 27 [](http://bitbucket.com/IonicaBizau)// => Index: 28 [](http://twitter.com/IonicaBizau)// => Index: 29 [](http://youtube.com/IonicaBizau)
❓ Get Help
There are few ways to get help:
- Please post questions on Stack Overflow. You can open issues with questions, as long you add a link to your Stack Overflow question.
- For bug reports and feature requests, open issues. 🐛
- For direct and quick help, you can use Codementor. 🚀
📝 Documentation
open(url, options, callback)
Opens the specified document.
Params
- String
url
: The page to open. - Object
options
: An object containing: include_jquery
(Boolean): Iffalse
, it will not re-inject jQuery on the page (default:true
).- Function
callback
: The callback function.
jQueryFn(ph, page, status)
This function creates the jQuery-like function.
Params
- Phantom
ph
: The Phantom object. - PhantomPage
page
: The Phantom page object. - String
status
: The page status (most probably"success"
).
Return
- Function The jQuery-like function.
wrapElement(elm)
Converts an jQueryInterface instance into jQuery-like function.
Params
- jQueryInterface
elm
: The jQueryInterface element.
Return
- Function The jQuery-like function.
addJQueryMethod(cField)
Extends the jQueryInterface with new fields.
Params
- Object
cField
: An object containing: field
(String): The field name.type
(String): The field type (e.g.function
).
jQueryInterface(ph, page, status, args)
This class provides a jQuery-like functionality, having an async interface.
Instances of this class will have the jQuery methods, that will be used like this:
text { // Do something with .my-class text};
Or if you like to set the text, provide the text string as first argument:
text"text to set" { // Do something after setting the text};
Note that as long the Phantom's evaluate
method is async, all these methods will be async.
Params
- Phantom
ph
: The Phantom object. - PhantomPage
page
: The Phantom page object. - String
status
: The page status (most probably"success"
). - Array
args
: The arguments to call the jQuery function with.
Return
- jQueryInterface The jQuery async interface.
eq(index, callback)
Get the element at specified index.
Params
- Number
index
: The index of the element. - Function
callback
: The callback function.
each(callback, done)
Iterate the elements.
Params
-
Function
callback
: The callback function (called for each element). You can use it like this:; -
Function
done
: This function is called when the elements' iteration was ended.
😋 How to contribute
Have an idea? Found a bug? See how to contribute.
💖 Support my projects
I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously, this takes time. You can integrate and use these projects in your applications for free! You can even change the source code and redistribute (even resell it).
However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it:
-
Starring and sharing the projects you like 🚀
-
—I love books! I will remember you after years if you buy me one. 😁 📖
-
—You can make one-time donations via PayPal. I'll probably buy a
coffeetea. 🍵 -
—Set up a recurring monthly donation and you will get interesting news about what I'm doing (things that I don't share with everyone).
-
Bitcoin—You can send me bitcoins at this address (or scanning the code below):
1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6
Thanks! ❤️
🍰 Thanks
This project is heavily inspired from jquery.go.js
by @travist. 🍰