gpt4 book ai didi

javascript - 浏览器标签通知

转载 作者:行者123 更新时间:2023-12-03 14:37:21 25 4
gpt4 key购买 nike

我一直在搜索如何在网站页面的选项卡上获取通知徽章,如下图所示,但找不到太多或任何有效的东西。我创建了一个聊天网站,希望人们知道他们所在的 channel 中是否有新通知。但我只需要知道如何在网站图标上获得红色徽章按钮或类似的东西。
最好是像 Discord 所做的那样:
enter image description here
我已经尝试过了,但似乎不起作用:

var count = 0;

var title = document.title;

function changeTitle() {
count++;
var newTitle = '(' + count + ') ' + title;
document.title = newTitle;
}

function newUpdate() {
update = setInterval(changeTitle, 2000);
}
var docBody = document.getElementById('site-body');
docBody.onload = newUpdate;

最佳答案

这应该让你开始。
Tl;博士:更新 <link>带有新图标图像的元素。
enter image description here
基本上它的作用:

  • <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" /> 获取图像的来源元素
  • 创建一个 内存 Canvas 元素
  • 应用网站图标图像
  • 在顶部绘制形状
  • 上绘制文本值
  • 更新<link>元素 href来自 的最终组合的属性 Canvas 元素图像数据

  • 像这样使用:
    const myBadgerOptions = {}; // See: constructor for customization options
    const myBadger = new Badger(myBadgerOptions);

    // Live update value example:
    myBadger.value = 3;

    // Remove badge example:
    // myBadger.value = 0;

    // If needed, get the generated base64 image data:
    // console.log(myBadger.dataURL);
    Badger构造函数:
    /**
    * Add notification badge (pill) to favicon in browser tab
    * @url stackoverflow.com/questions/65719387/
    */
    class Badger {
    constructor(options) {
    Object.assign(
    this, {
    backgroundColor: "#f00",
    color: "#fff",
    size: 0.6, // 0..1 (Scale in respect to the favicon image size)
    position: "ne", // Position inside favicon "n", "e", "s", "w", "ne", "nw", "se", "sw"
    radius: 8, // Border radius
    src: "", // Favicon source (dafaults to the <link> icon href)
    onChange() {},
    },
    options
    );
    this.canvas = document.createElement("canvas");
    this.src = this.src || this.faviconEL.getAttribute("href");
    this.ctx = this.canvas.getContext("2d");
    }

    faviconEL = document.querySelector("link[rel$=icon]");

    _drawIcon() {
    this.ctx.clearRect(0, 0, this.faviconSize, this.faviconSize);
    this.ctx.drawImage(this.img, 0, 0, this.faviconSize, this.faviconSize);
    }

    _drawShape() {
    const r = this.radius;
    const xa = this.offset.x;
    const ya = this.offset.y;
    const xb = this.offset.x + this.badgeSize;
    const yb = this.offset.y + this.badgeSize;
    this.ctx.beginPath();
    this.ctx.moveTo(xb - r, ya);
    this.ctx.quadraticCurveTo(xb, ya, xb, ya + r);
    this.ctx.lineTo(xb, yb - r);
    this.ctx.quadraticCurveTo(xb, yb, xb - r, yb);
    this.ctx.lineTo(xa + r, yb);
    this.ctx.quadraticCurveTo(xa, yb, xa, yb - r);
    this.ctx.lineTo(xa, ya + r);
    this.ctx.quadraticCurveTo(xa, ya, xa + r, ya);
    this.ctx.fillStyle = this.backgroundColor;
    this.ctx.fill();
    this.ctx.closePath();
    }

    _drawVal() {
    const margin = (this.badgeSize * 0.18) / 2;
    this.ctx.beginPath();
    this.ctx.textBaseline = "middle";
    this.ctx.textAlign = "center";
    this.ctx.font = `bold ${this.badgeSize * 0.82}px Arial`;
    this.ctx.fillStyle = this.color;
    this.ctx.fillText(this.value, this.badgeSize / 2 + this.offset.x, this.badgeSize / 2 + this.offset.y + margin);
    this.ctx.closePath();
    }

    _drawFavicon() {
    this.faviconEL.setAttribute("href", this.dataURL);
    }

    _draw() {
    this._drawIcon();
    if (this.value) this._drawShape();
    if (this.value) this._drawVal();
    this._drawFavicon();
    }

    _setup() {
    this.faviconSize = this.img.naturalWidth;
    this.badgeSize = this.faviconSize * this.size;
    this.canvas.width = this.faviconSize;
    this.canvas.height = this.faviconSize;
    const sd = this.faviconSize - this.badgeSize;
    const sd2 = sd / 2;
    this.offset = {
    n: {x: sd2, y: 0 },
    e: {x: sd, y: sd2},
    s: {x: sd2, y: sd},
    w: {x: 0, y: sd2},
    nw: {x: 0, y: 0},
    ne: {x: sd, y: 0},
    sw: {x: 0, y: sd},
    se: {x: sd, y: sd},
    }[this.position];
    }

    // Public functions / methods:

    update() {
    this._value = Math.min(99, parseInt(this._value, 10));
    if (this.img) {
    this._draw();
    if (this.onChange) this.onChange.call(this);
    } else {
    this.img = new Image();
    this.img.addEventListener("load", () => {
    this._setup();
    this._draw();
    if (this.onChange) this.onChange.call(this);
    });
    this.img.src = this.src;
    }
    }

    get dataURL() {
    return this.canvas.toDataURL();
    }

    get value() {
    return this._value;
    }

    set value(val) {
    this._value = val;
    this.update();
    }
    }

    关于javascript - 浏览器标签通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65719387/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com