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

activex-helpers

v1.0.3

Published

Event handler management, and parameterized property setters, for ActiveX objects

Downloads

5,792

Readme

Also available via npm.

Event handler management

There are a number of mechanisms for handling ActiveX events in Javascript; however, they all require that:

  • the variable must be initialized before the function declaration is evaluated. This is harder than it seems, because of function declaration hoisting; and requires doing one of the following:
    • wrap the function declaration within an IIFE (this works because function declarations are only hoisted to the function scope),
    • some form of string-to-code evaluation -- eval, setTimeout, window.execScript, new Function, or
    • wrap the function within a SCRIPT block, while the initialization happens before the SCRIPT block (either in another SCRIPT block, or by setting the id of a previous element); this also implies that the id/variable must be available to the global scope.
  • the function must have a special name -- depending on the environment and event handling mechanism, either variable.eventName, variable::eventName, or variable_eventName
  • the function must be a function declaration, not a function expression
  • the parameters of the function must exactly match those defined in the ActiveX event
var wdApp = new ActiveXObject('Word.Application');

//using eval
eval('function wdApp::Quit() {window.alert(\'Application quit\');}');

//using an IIFE
(function() {
  function wdApp::Quit() {
    window.alert('Application quit');
  }
})();

This library enables the following:

(function() {
  var wdApp = new ActiveXObject('Word.Application');
  
  //using a function expression, without a special name
  ActiveXObject.on(wdApp, 'Quit', function() {
    window.alert('Application quit');
    
    //`this` binding
    window.alert(this.Version);
  });

  //when the event is defined as passing parameters, the parameters are wrapped into a single object
  //the object is passed into the final event handler
  //AFAIK there is no way to determine the parameters at runtime, so the names must be passed in at registration
  ActiveXObject.on(wdApp, 'DocumentBeforeSave', ['Doc','SaveAsUI','Cancel'], function (params) {
    //changes to the `params` object are propagated back to the internal handler
    params.SaveAsUI = false;
    params.Cancel = !window.confirm("Do you really want to save?");   
  });
})();

Property setter

Property getters and setters without parameters are represented as Javascript simple read/write properties. However, while getters with paraneters are represented as methods, setters with parameters are represented as assignment to methods.

var dict = new ActiveXObject('Scripting.Dictionary');

//setter with parameters
dict.Item('a') = 1;

This is non-standard Javascript. The library enables calling setters with parameters in a standard Javascript-compliant fashion:

ActiveXObject.set(dict, 'Item', ['a'], 1);

Usage

This library relies on a number of ES5 array methods. The necessary shims are available in shims.js, in the absence of another shim library.