phantom-jquery

1.2.11 • Public • Published

phantom-jquery

phantom-jquery

Support me on Patreon Buy me a book PayPal Ask me anything Version Downloads Get help on Codementor

Buy Me A Coffee

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

// Dependencies
var phJQuery = require("phantom-jquery");
 
// Open my website
phJQuery.open("http://ionicabizau.net", (err, $, page, ph) => {
 
    // Handle error
    if (err) { return console.log(err); }
 
    // Iterate the link elements
    $("a").each((currentLink, index, next) => {
 
        // Get the current link text
        currentLink.text(text => {
 
            // Get the curent link href
            currentLink.attr("href", href => {
 
                // Show the text and the href
                console.log("Index: " + index + " [" + text + "](" + href + ")")
 
                // Go to the next link
                next();
            });
        });
    }, () => {
        ph.exit();
    });
});
 
// => 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:

  1. Please post questions on Stack Overflow. You can open issues with questions, as long you add a link to your Stack Overflow question.
  2. For bug reports and feature requests, open issues. 🐛
  3. 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): If false, 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:

$(".my-class").text(function (text) {
  // Do something with .my-class text
});

Or if you like to set the text, provide the text string as first argument:

$(".my-class").text("text to set", function () {
  // 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:

    $(".items .item").each(function (current_item, index, next) {
        current_item.text(function (text) {
           // Do something with the item's text, then go to the next element
           console.log(text);
           next();
        });
    }, function () {
        console.log("All elements were iterated.");
    });
  • 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 🚀

  • Buy me a book—I love books! I will remember you after years if you buy me one. 😁 📖

  • PayPal—You can make one-time donations via PayPal. I'll probably buy a coffee tea. 🍵

  • Support me on Patreon—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. 🍰

📜 License

MIT © Ionică Bizău

Readme

Keywords

Package Sidebar

Install

npm i phantom-jquery

Weekly Downloads

9

Version

1.2.11

License

MIT

Unpacked Size

26.1 kB

Total Files

5

Last publish

Collaborators

  • ionicabizau