PPML

Pixel Place Map Loader

Tính đến 09-05-2022. Xem phiên bản mới nhất.

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://update.greasyforks.org/scripts/443803/1048549/PPML.js

// ==UserScript==
// @name         PPML
// @description  Pixel Place Map Loader
// @version      1.5.1
// @author       0vC4
// @namespace    https://greasyforks.org/users/670183
// @match        https://pixelplace.io/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=pixelplace.io
// @license      MIT
// @grant        none
// @run-at       document-start
// ==/UserScript==





const PPML = (() => {
    if (window.PPML) return window.PPML;
    const log = console.log;
  
  
  
    const PPML = {
        config({colors}) {
            Object.assign(PPML, {
                colors
            });
        },
        map: {},
        _hooks: [],
        set onload(func) {
            this._hooks.push(func);
        }
    };
  
  
  
    const Img = window.Image;
    window.Image = function() {
        const img = new Img(...arguments);
  
        Object.defineProperty(img, 'src', {
            enumerable: true,
            configurable: true,
            set(val) {
                this.setAttribute('src', val);
                if (!val.match(/canvas\/\d+\.png\?/)) return;
  
                this.addEventListener('load', () => {
                    const canvas = document.createElement('canvas');
                    canvas.width = this.width;
                    canvas.height = this.height;
  
                    const ctx = canvas.getContext('2d');
                    ctx.drawImage(this, 0, 0);
  
                    const rgba = ctx.getImageData(0, 0, this.width, this.height).data;
                    const pixels = new Uint8Array(rgba.length>>2);
                    for (let i = 0; i < rgba.length; i += 4)
                        pixels[i>>2] = PPML.colors.indexOf((rgba[i]<<16) + (rgba[i+1]<<8) + (rgba[i+2]))-1;
  
                    const {width, height} = this;
                    const map = {
                        pixels, width, height,
                        get(x,y) {
                            return this.pixels[x+y*this.width];
                        },
                        set(x,y,pixel) {
                            const offset = x+y*this.width;
                            if (this.pixels[offset] == null) return;
                            this.pixels[offset] = pixel;
                        }
                    };
  
                    Object.assign(PPML.map, map);
                    PPML._hooks.map(f => f(map));
                });
            }
        });
  
        return img;
    };
    Object.assign(Image, Img);
    for (let k in Img.prototype) try {Image.prototype[k] = Img.prototype[k];} catch (e) {};
  
  
  
    window.PPML = PPML;
    return PPML;
  })();
  // 0vC4#7152
长期地址
遇到问题?请前往 GitHub 提 Issues。