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

set-img-src

v1.3.1

Published

Sets the src of an image element without leaking memory.

Downloads

9

Readme

Set-Img-Src

Github

GitHub CodeFactor AppVeyor tests npm bundle size GitHub top language Code Style

Table of contents

General info

Set-img-src is aimed at solving a browser bug where changing the src attribute of an image between dataURLs or base64-strings causes RAM spikes. The bug has been marked as fixed in Chromium however people have reported it despite that and I myself recently encountered it as well. Essentially all this project does is taking this solution of using cloneNode, adding a parameter for moving eventListeners as well, and then putting it in a package so it can be easily moved between projects.

Technologies

Project is created with:

  • Typescript version: 4.6

Setup

To run this project, install it locally using npm:

$ npm install set-img-src

Documentation

Set-img-src's default export is a class with static functions for setting src by Id or Element. The passed value can be either formatted as a base64-string or a dataUrl.

import ImgSrcHandler from 'set-img-src'
...
function ById(id: string, value: string) {
    ImgSrcHandler.setSrcById(id, value);
}

function byElement(id: string, value: string) {
    element = document.getElementById(id);
    ImgSrcHandler.setSrcByElement(element, value);
}

If your element has event listeners attached to it you will have to pass these through the 'eventProperties' parameter as Node.cloneNode does not copy them automatically.

import ImgSrcHandler from 'set-img-src'
...
function setWithEvents(id: string, value: string) {
    ImgSrcHandler.setSrcById(id, value, {Event: 'click', Listener: handleClick});
}

There is also a class with static async functions that returns promises if that is more to your liking.

import {ImgSrcAsyncHandler} from 'set-img-src'
...
function asyncById(id: string, value: string) {
    ImgSrcAsyncHandler.setSrcById(id, value).then(() => {
        console.log('Completed!')
    })
}

You can also import functions one by one,

import {setSrcById, removeSrcById} from 'set-img-src'
...
function ById(id: string, value: string) {
    setSrcById(id, string);
}

function remove(id) {
    removeSrcById(id);
}

Which also goes for async functions.

import {setSrcByIdAsync} from 'set-img-src'
...
function asyncById(id: string, value: string) {
    setSrcByIdAsync(id, string).then(() => {
        console.log('Completed!');
    })
}

All Exports

default class ImgSrcHandler {
    static setSrcById(id: string, value: string, eventProperties?: EventProperty[]): void;
    static removeSrcById(id: string, eventProperties?: EventProperty[]): void;
    static setSrcByElement(image: HTMLImageElement, value: string, eventProperties?: EventProperty[]): void;
    static removeSrcByElement(image: HTMLImageElement, eventProperties?: EventProperty[]): void;
}

class ImgSrcAsyncHandler {
    static setSrcById(id: string, value: string, eventProperties?: EventProperty[]): Promise<void>;
    static removeSrcById(id: string, eventProperties?: EventProperty[]): Promise<void>;
    static setSrcByElement(image: HTMLImageElement, value: string, eventProperties?: EventProperty[]): Promise<void>;
    static removeSrcByElement(image: HTMLImageElement, eventProperties?: EventProperty[]): Promise<void>;
}

function setSrcById(id: string, value: string, eventProperties?: EventProperty[]): void;

function removeSrcById(id: string, eventProperties?: EventProperty[]): void;

function setSrcByElement(image: HTMLImageElement, value: string, eventProperties?: EventProperty[]): void;

function removeSrcByElement(image: HTMLImageElement, eventProperties?: EventProperty[]): void;

function setSrcByIdAsync(id: string, value: string, eventProperties?: EventProperty[]): Promise<void>;

function removeSrcByIdAsync(id: string, eventProperties?: EventProperty[]): Promise<void>;

function setSrcByElementAsync(image: HTMLImageElement, value: string, eventProperties?: EventProperty[]): Promise<void>;

function removeSrcByElementAsync(image: HTMLImageElement, eventProperties?: EventProperty[]): Promise<void>;

interface EventProperty {
    Event: string;
    Listener: EventListenerOrEventListenerObject;
    Options?: boolean | AddEventListenerOptions | undefined;
}