写作绅士,读作丧尸 X岛揭示板
顺猴者昌 逆猴者亡 首页版规 |用户系统 |移动客户端下载 | 丧尸路标 | | 常用图串及路标 | 请关注 官方公众号:【X岛揭示板】 官方微博: 【@X岛极速版】| 人,是会思考的芦苇
常用串:·豆知识·跑团板聊天室·公告汇总串·X岛路标

No.53180931 - 无标题 - 技术宅


回应模式
No.53180931
名 称
E-mail
标题
颜文字
正文
附加图片
•程序语言、压制投稿、视频制作以及各计算机领域的技术问题
•我觉得还是CSDN靠谱一点
•本版发文间隔为15秒。

收起 查看大图 向左旋转 向右旋转
无标题 无名氏 2022-11-03(四)17:58:22 ID:w0w8qk3 [举报] [订阅] [只看PO] No.53180931 [回应] 管理
油猴脚本,给每个uid生成一个随机头像
无标题 无名氏 2022-11-03(四)18:00:57 ID:w0w8qk3 (PO主) [举报] No.53180984 管理
(function () {
'use strict';
setTimeout(() => {
const nodes = document.getElementsByClassName('h-threads-info-uid');
for (let i = 0; i < nodes.length; i++) {
const node = nodes.item(i);
createHeadImg(node)
}
}, 1);
})();

class Color {
r = 0;
g = 0;
b = 0;
constructor(r, g, b) {
if (r === undefined) {
return;
}
if (g === undefined && b === undefined) {
if (typeof r == 'string') {
const str = r;
let startIdx = 0;
if (str0[0,0] == '#') {
startIdx = 1;
}
this.r = parseInt(str.substring(startIdx, startIdx + 2), 16);
this.g = parseInt(str.substring(startIdx + 2, startIdx + 2 + 2), 16);
this.b = parseInt(str.substring(startIdx + 4, startIdx + 4 + 2), 16);
return;
}
if (typeof r == 'number') {
this.r = r % 256;
this.g = Math.floor(r / 256) % 256;
this.g = Math.floor(Math.floor(r / 256) / 256) % 256;
return;
}
}
this.r = r;
this.g = g;
this.b = b;
}
toString() {
return '#' + this.r.toString(16).padStart(2, '0') + this.g.toString(16).padStart(2, '0') + this.b.toString(16).padStart(2, '0');
}
}

function createHeadImg(node) {
const canvas = document.createElement('canvas');
canvas.width = 20;
canvas.height = 20;
canvas.style.marginLeft.width = '5px';
const ctx = canvas.getContext('2d');
const imgData = ctx.createImageData(canvas.width, canvas.height);
for (let y = 0; y < canvas.height; y++) {
for (let x = 0; x < canvas.width; x++) {
imgData.data[(y * canvas.width + x) * 4 + 0] = 220;
imgData.data[(y * canvas.width + x) * 4 + 1] = 220;
imgData.data[(y * canvas.width + x) * 4 + 2] = 220;
imgData.data[(y * canvas.width + x) * 4 + 3] = 255;
}
}
ctx.putImageData(imgData, 0, 0);

const charMap = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const str = node.innerHTML.substring(3);
let number = BigInt(0);
for (let i = 0; i < str.length; i++) {
let a = BigInt(charMap.indexOf(str[i]));
for (let j = 0; j < i; j++) {
a = a * BigInt(charMap.length);
}
number = number + a;
}
const show = Number(number % BigInt(2 ^ 36));
for (let y = 0; y < 6; y++) {
for (let x = 0; x < 6; x++) {
const color = new Color(Number(number % BigInt(256 * 256 * 256)));
if (Math.floor(show / (2 ^ (y * 6 + x))) % 2 == 0) {
ctx.fillStyle = color.toString();
} else {
const number = Math.floor((255 - color.r + 255 - color.g + 255 - color.b) / 3);
const backColor = new Color(number, number, number);
ctx.fillStyle = backColor.toString();
}
ctx.fillRect(1 + x * 3, 1 + y * 3, 3, 3);
}
}
node.appendChild(canvas);
}

function RandomInt(a, b = 0) {
const max = Math.max(a, b);
const min = Math.min(a, b);
return Math.floor(Math.random() * (max - min) + min);
}
无标题 无名氏 2022-11-03(四)18:47:46 ID:2EAXtFY [举报] No.53182007 管理
( ゚∀。)
无标题 无名氏 2022-11-03(四)22:44:17 ID:w0w8qk3 (PO主) [举报] No.53188721 管理
(function () {
'use strict';
setTimeout(() => {
const nodes = document.getElementsByClassName('h-threads-info-uid');
for (let i = 0; i < nodes.length; i++) {
const node = nodes.item(i);
createHeadImg(node)
}
}, 1);
})();

class Color {
r = 0;
g = 0;
b = 0;
constructor(r, g, b) {
if (r === undefined) {
return;
}
if (g === undefined && b === undefined) {
if (typeof r == 'string') {
const str = r;
let startIdx = 0;
if (str0[0,0] == '#') {
startIdx = 1;
}
this.r = parseInt(str.substring(startIdx, startIdx + 2), 16);
this.g = parseInt(str.substring(startIdx + 2, startIdx + 2 + 2), 16);
this.b = parseInt(str.substring(startIdx + 4, startIdx + 4 + 2), 16);
return;
}
if (typeof r == 'number') {
this.r = r % 256;
this.g = Math.floor(r / 256) % 256;
this.g = Math.floor(Math.floor(r / 256) / 256) % 256;
return;
}
}
this.r = r;
this.g = g;
this.b = b;
}
toString() {
return '#' + this.r.toString(16).padStart(2, '0') + this.g.toString(16).padStart(2, '0') + this.b.toString(16).padStart(2, '0');
}
}

function createHeadImg(node) {
const canvas = document.createElement('canvas');
canvas.width = 20;
canvas.height = 20;
canvas.style.marginLeft.width = '5px';
const ctx = canvas.getContext('2d');
const imgData = ctx.createImageData(canvas.width, canvas.height);
for (let y = 0; y < canvas.height; y++) {
for (let x = 0; x < canvas.width; x++) {
imgData.data[(y * canvas.width + x) * 4 + 0] = 220;
imgData.data[(y * canvas.width + x) * 4 + 1] = 220;
imgData.data[(y * canvas.width + x) * 4 + 2] = 220;
imgData.data[(y * canvas.width + x) * 4 + 3] = 255;
}
}
ctx.putImageData(imgData, 0, 0);

const charMap = '0123stuvwxyzABCDEFGHIJ456789abcdefghijklmnopqrKLMNOPQRSTUVWXYZ';
const str = node.innerHTML.substring(3);
let number = BigInt(0);
for (let i = 0; i < str.length; i++) {
let a = BigInt(charMap.indexOf(str[i]));
number = number * BigInt(charMap.length) + a;
}
let show = hash(number, BigInt(2 ^ 36));
for (let y = 0; y < 6; y++) {
for (let x = 0; x < 6; x++) {
const color = new Color(hash(number, 256 * 256 * 256));
if (Math.floor(show / (2 ^ (y * 6 + x))) % 2 == 0) {
ctx.fillStyle = color.toString();
} else {
let number = Math.floor((128 + color.r * 0.299 + color.g * 0.587 + color.b * 0.114) % 255);
const backColor = new Color(number, number, number);
ctx.fillStyle = backColor.toString();
}
ctx.fillRect(1 + x * 3, 1 + y * 3, 3, 3);
}
}
node.appendChild(canvas);
}

function hash(number, modNumber) {
let str = number.toString(2);
const length = modNumber.toString(2).length;
let result = 0;
while (str.length > length) {
let subStr = str.substring(0, length);
result = result ^ parseInt(subStr, 2);
str = str.substring(length);
}
return result;
}

function RandomInt(a, b = 0) {
const max = Math.max(a, b);
const min = Math.min(a, b);
return Math.floor(Math.random() * (max - min) + min);
}
无标题 无名氏 2022-11-03(四)22:45:37 ID:w0w8qk3 (PO主) [举报] No.53188760 管理
>>No.53188721
重写了散列函数,更加随机化~
重写了颜色算法,生成对比更加明显的背景色
无标题 无名氏 2022-11-03(四)22:50:52 ID:FmjnNP4 [举报] No.53188922 管理
( ゚∀。)
无标题 无名氏 2022-11-03(四)22:55:55 ID:w0w8qk3 (PO主) [举报] No.53189072 管理
两个 ( ゚∀。) 是肿么回事?!(´゚Д゚`)
无标题 无名氏 2022-11-05(六)06:04:34 ID:xjAmHlQ [举报] No.53218592 管理
你其实可以把代码上传到github,然后把git仓库的链接发给我们。
无标题 无名氏 2022-11-05(六)22:51:26 ID:OQwBjJk [举报] No.53238263 管理
有点抽象( ゚∀。)

以及,发代码其实可以用 pastebin 的吧
无标题 无名氏 2022-11-05(六)23:08:27 ID:bRRnPeX [举报] No.53238710 管理
好活 ( ゚∀。)
可以考虑用现成的各种identicon库,也是输入任意字符串返回这种头像的工具|∀゚
无标题 无名氏 2023-01-01(日)07:39:41 ID:T2AdD2W [举报] No.54522182 管理
好像之前也有根据饼干,自动替换中文昵称。

UP主: