npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wson-event-connector

v0.5.165

Published

Serialize DOM events with wson.

Downloads

36

Readme

wson-event-connector

Build Status Dependency Status devDependency Status NPM version

WSON is a human-readable data-interchange format with support for cyclic structures. This module is an extension to wson that enables serializing DOM events to strings and parsing those strings back to DOM events.

Possible Use Cases

  1. Record DOM events to later simulate a user during automated test (needs wson-dom-connector),
  2. Log DOM events just for debugging.

Installation

npm install --save wson wson-event-connector

Usage

It can be used in a web browser via browserify...

var WSON = require("wson").Wson;
var eventConnectors = require("wson-event-connector");

var wson = new WSON({
  connectors: eventConnectors(window)
  });

function logEvent(e) {
  console.log(wson.stringify(e));
}

var events = [ 'load', 'error', 'focus', 'blur', 'resize', 'scroll', 'unload' ];
events.forEach(function(name) {
  window.addEventListener(name, logEvent);
});

...or in node with any standard-compliant DOM implementation (e.g. jsdom).

var WSON = require("wson").Wson;
var eventConnectors = require("wson-event-connector");
var domConnectors = require("wson-dom-connector");
var jsdom = require("jsdom");
var _ = require("underscore");

var window = jsdom.jsdom("<body></body>").defaultView;

var wson = new WSON({
  connectors: _.extend(eventConnectors(window), domConnectors(window))
  });

var body = window.document.body;
body.addEventListener('click', function(event) {
  console.log(wson.stringify(event)));
}
body.dispatchEvent(new window.MouseEvent('click', {
  screenX: 300,
  screenY: 400,
  clientX: 20,
  clientY: 10,
  button: 1,
  buttons: 1,
});
// [:MouseEvent|click|#f|#f|#0|#0|#300|#400|#20|#10|#1|#1|#n|#n|[:HTMLBodyElement|/body`a1`e]]

Above example uses connectors from wson-dom-connector module to serialize DOM nodes assigned to event properties.

Supported Events

Following events types are currently supported:

(Not Yet) Supported Events

Near future should bring support for following classes:

Feel free to message me if you desperately need one of above.

Pull requests are also very welcome!

CONTRIBUTING GUIDELINES:

Please do not submit pull requests implementing non-standard vendor-specific events. For those, a separate module should be created (e.g. wson-mozilla-connector), with this module as dependency (see API Reference).

Unsupported Events

Serialization of following event classes will not be implemented in this module:

Why are some properties not serialized?

Following properties are by default not serialized:

API Reference

This document describes API exported by this (wson-event-connector) module. Please refer to wson's documentation for description of wson's API and serialization algorithm.

All Connectors

exports = function(window, additionalFields = []) { ... }

Creates WSON connectors for all event classes found in window namespace. Created connectors will be extended to serialize fields passed in additionalFields array. Function returns a map (event class name => connector instance), which can be passed as "connectors" option to WSON's constructor (see example below).

var WSON = require('wson').Wson;
var eventConnectors = require('wson-event-connector');

var wson = new WSON({ connectors: eventConnectors(window) });

Event Connector

exports.Event = function(EventClass, additionalFields = []) { ... }

Constructs a connector which is able to serialize instances of EventClass. Passed class must be derived from or equal window.Event. Returned connector serializes fields in following order: Event.bubbles, Event.cancelable, additionalFields..., Event.target.

Event.target is not settable from JavaScript. Web browsers assign its value inside EventTarget.dispatchEvent(event). Events returned from wson.parse(string) are not yet dispatched, hence they do not have Event.target set. Instead, target is deserialized into non-standard Event.parsedTarget property (see example below).

var WSON = require('wson').Wson;
var eventConnectors = require('wson-event-connector');
var domConnectors = require('wson-dom-connector');

var wson = new WSON({ connectors: {
  'Event': eventConnectors.Event(window.Event),
  'HTMLBodyElement': domConnectors(window).HTMLBodyElement,
  }});

var event = wson.parse('[:Event|load|#f|#t|[:HTMLBodyElement|/body`a1`e]]');
event.parsedTarget.dispatchEvent(event);

Init Based Connector

exports.InitBased = function(Class, serializedFields) { ... }

Constructs a connector which is able to serialize instances of Class. Class' constructor must accept single argument, which is a map containing initial values for properties of constructed object (init object pattern?). Constructed connector serializes fields of names and in order as passed in serializedFields array.

var WSON = require('wson').Wson;
var connectors = require('wson-event-connector');

var wson = new WSON({ connectors: {
  'Weather': new connectors.InitBased(Weather, [ 'temperature', 'pressure', 'humidity', 'sky' ])
  }});

var weather = new Weather({
  temperature: '27C',
  pressure: '1000HpA',
  humidity: '75%',
  sky: 'clear'
});
console.log(wson.stringify(weather));
// [:Weather|27C|1000Hpa|75%|clear]

License

Copyright © 2016 - 2019 Maciej Chałapuk. Released under MIT license.