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

descop

v1.0.4

Published

A small utility Library allowing you to get caret [start, end] offset position in HTML source code for selected element

Downloads

2

Readme

Basic Overview

Descop is a library which allows you to find a source code position of target document element in standardized way.

Note: it works only with a static document without scripts or any dynamic markup. If you need to find an element inside a dynamic document - use a some kind a Static to Dynamic DOM mapper first. Or, simply wait a bit - i'm on my way to create a one for you.

  • Eugene Ford, author

Library can be used both in Node.js and directly in Browser. It is well-documented and completely covered with test specs (excluding webpack bundling definitions and external modules).

If you want to add some features or to suggest any idea, feel free - contributions are always welcome.

How to Install

Using NPM

To use Descop with NPM simply call:

npm install --save descop

In Browser

To use Descop directly in browser simply download this repository and copy dist/descop.js into your project. Next, include it on your .html page:

<script src="path/to/your/js/descop.js"></script>

Get Started

First, there are two simple conditions to use Descop:

  1. Your html source must be Valid. (use w3c validator for example)
  2. Your document must be a static one.

That's all.

You are able to use Descop as the importable npm package or directly in browser.

In Node.js

import Descop from "descop";

// Get the source html code of target document
var html = yourFunctionToGetHTML();

// Get the target document itself
var dom = yourFunctionToGetDocument();

// Get the element you want to found in source code
var element = document.getElementById("target-element");

// Create an instance of Descop
var descop = new Descop();

// Connect document
descop.connectDocument(dom);

// Connect source code
descop.connectSource(html);

// Get element position in source code
var position = descop.getElementPosition(element);
// eg. position => { start: 320, end: 480 }

In Browser

<script>
// Get the source html code of target document
var html = yourFunctionToGetHTML();

// Get the target document itself
var dom = yourFunctionToGetDocument();

// Get the element you want to found in source code
var element = document.getElementById("target-element");

// Create an instance of Descop
var descop = new Descop();

// Connect document
descop.connectDocument(dom);

// Connect source code
descop.connectSource(html);

// Get element position in source code
var position = descop.getElementPosition(element);
// eg. position => { start: 320, end: 480 }
</script>

API

connectSource(html)

Connects an html source to Descop instance

var descop = new Descop();
var html = "<!DOCTYPE html><title > Example </title ><div>Hello World</div>";
descop.connectSource(html);

connectDocument(document)

Connects a document to Descop instance

var descop = new Descop();
descop.connectDocument(document);

findFragmentPosition(fragment, fromIndex)

Returns the position (start and end offset) of the first occurrence of specified html fragment inside connected html source. Fragment - html string to search for, FromIndex - optional index to start search from

var descop = new Descop();
...

var position = descop.findFragmentPosition("<title>Example</title>");
// position => { start: 15, end: 41 }

findFragment(fragment, fromIndex)

Returns the first occurrence of specified html fragment inside connected html source. Fragment - html string to search for, FromIndex - optional index to start search from.

var descop = new Descop();
...

var html = descop.findFragment("<title>Example</title>");
// html => "<title > Example </title >"

findElementPosition(element)

Returns the position (start and end offset) of target element in source html code. Element - element to search for.

var descop = new Descop();
...
var element = document.querySelector("div");

var position = descop.findElementPosition(element);
// position => { start: 41, end: 63 }

findElement(element)

Returns the source html code of target element. Element - element to search for.

var descop = new Descop();
...
var element = document.querySelector("div");

var html = descop.findElement(element);
// html => "<div>Hello World</div>"

getSource()

Gets current html source.

var descop = new Descop();

...

var source = descop.getSource();

getDocument()

Gets current document.

var descop = new Descop();

...

var doc = descop.getDocument();

Contributing to Descop

Contributions are always welcome. Before contributing please read the code of conduct & search the issue tracker (your issue may have already been discussed or fixed).

To contribute, follow next steps:

  • Fork Descop
  • Commit your changes
  • Open a Pull Request.

Feature Requests

Feature requests should be submitted in the issue tracker, with a description of the expected behavior & use case, where they'll remain closed until sufficient interest (e.g. :+1: reactions). Before submitting a feature request, please search for similar ones in the closed issues.

License

Released under the MIT License