Greasy Fork is available in English.

HtmlId

Class representing an "id" Attribut to be associated to a HTML-Element

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://greasyfork.org/scripts/391549-htmlid/code/HtmlId.js?version=744142

// ==UserScript==
// @name         HtmlId
// @namespace    hoehleg.userscripts.private
// @version      0.1
// @description  Class representing an "id" Attribut to be associated to a HTML-Element
// @author       Gerrit Höhle
// @grant        none
// ==/UserScript==

/* jshint esversion: 6 */
/* globals $, jQuery */
const HtmlId = (() => {
    'use strict';
    const _value = Symbol("value");

    return class HtmlId {

        /**
         * Creates a HtmlId- object.
         *
         * @param {String} idValue - value of the Id-Attribute
         */
        constructor(idValue) {
            this[_value] = String(idValue);
        }

        /*
         * @returns {Element} - The dom element or null if none exists
         */
        get element() {
            return document.getElementById(this);
        }

        /*
         * @returns {String} - the CSS Selector
         */
        get selector() {
            return `#${this}`;
        }

        /*
         * @returns {Object} - The JQuery object wrapping the current dom element, or an empty JQuery object if none exists
         */
        get $() {
            return $(this.element);
        }

        /*
         * Removes the id from the current dom element, if such exists
         *
         * @returns {HtmlId} - passthrough of this HtmlId
         */
        remove() {
            const curElement = this.element;
            if (curElement) {
                curElement.removeAttribute("id");
            }
            return this;
        }

        /*
         * Adds this id as an attribut to a new dom Element. Removes the id from the current dom element, if such exists.
         *
         * @param {Element} newElement - new Dom Element to which to assign this id
         *
         * @returns {HtmlId} - passthrough of this HtmlId
         */
        assign(newElement) {
            this.remove();
            if (newElement && typeof newElement.setAttribute === "function") {
                newElement.setAttribute("id", this);
            } else if (jQuery && newElement instanceof jQuery) {
                newElement.prop("id", this);
            }
           return this;
        }

        /*
         * @returns {String} -  value of this Attribute
         */
        toString() {
            return this[_value];
        }

        /*
         * @returns {String} -  value of this Attribute
         */
        valueOf() {
            return this[_value];
        }
    };
})();