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

knockout-text-captcha

v1.0.7

Published

Text captcha implemntation using knockout custom binding.

Downloads

4

Readme

Implement Captcha using knockout-typescript

  1. Captcha-Jquery lib
  2. Knockout custom binding for captcha
  3. Integrate Captcha Component in your project

Demo

Captch Demo

Show me some code

Configure Captcha and define isCaptchaValid() and refreshCaptcha() function

import * as ko from 'knockout';
import { BindingHandler, ObservableArray } from 'knockout';
import Captcha from '../../external/jquery-captcha';

const captchaBinding = {
    init: (elm: any, va: () => any, all: () => any, vm: any) => {

        const cmp: any = va();
        const cp = new Captcha(elm, cmp.captchaConfig);

        cmp.isCaptchaValid = () => {
            const isValid = cp.valid(cmp.captchaInput());
            // Refresh captcha if its not valid
            if (!isValid) {
                cp.refresh();
                cmp.captchaInput('');
            }
            return isValid;
        }

        cmp.refreshCaptcha = () => {
            cp.refresh();
            cmp.captchaInput('');
            cmp.clearError();
        }
    }

}as BindingHandler;

export default captchaBinding;

Captcha component which uses captcha binding

class CaptchaComponent  {
        captchaInput: Observable<string>;
        disableFields: Observable<boolean>;
        showSuccessMsg: Observable<boolean>;
        captchaConfig: any;
        isCaptchaValid: (() => boolean) | undefined;
        refreshCaptcha!: () => void;

        constructor(params: any) {
            this.disableFields = params.disable;
            this.captchaConfig= params.config? params.config :{
                width: 100,
                height: 40,
                font: 'bold 23px Arial',
                resourceType: 'aA0@',
                resourceExtra: [],
                caseSensitive: true,
                autoRefresh: false,
                clickRefresh: false
            };
            this.showSuccessMsg = ko.observable(false);
            this.captchaInput = ko.observable('').extend({
                validation: {
                    validator: () => {
                        if (this.isCaptchaValid) {
                            const isValid=this.isCaptchaValid();
                            this.showSuccessMsg(isValid);
                            return isValid;
                        }
                        return false;
                    },
                    message: "Please Enter Valid Captcha",
                }
            });
        }

        refresh() {
            if (this.refreshCaptcha) {
                this.refreshCaptcha();
            }
        }

       clearError(): void {
            let errors = ko.validation.group(this);
            errors.showAllMessages(false);
        }
    }

    export default {
    viewModel: CaptchaComponent,
    template: `
      <div class="form-group" style="background-color: floralwhite">
         <canvas id="canvas" data-bind="captcha: $data" ></canvas>
     </div>
     <div class="form-group">
        <div class="input-group" data-bind="validationOptions:{ insertMessages: false }">
          <input data-bind="value: captchaInput,disable: disableFields" class="form-control" style="color: black" placeholder="Enter Above Text Here And Press Enter" />
          <div class="input-group-append">
              <button class="btn btn-primary"
                     data-bind="click: refresh, disable: disableFields" data-toggle="tooltip" data-placement="top" title="Refresh Captcha">
                  <span>Refresh</span></button>
          </div>
        </div>
        <span class="text-danger" data-bind="validationMessage: captchaInput"></span>
        <span class="text-success" data-bind="visible: showSuccessMsg">Hurray!! Valid Captcha</span>
    </div>
`};

Refer External > jquery-captcha.ts file for implementation of captcha

Reference

I have refered https://github.com/honguangli/jquery-captcha and fixed some issue for refresh function and added some extra fetures to it to implement captcha in knockout.