拼音转换
// ==UserScript==
// @name 拼音转换
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.4
// @description Ctrl+x唤出面板,可在脚本菜单中设置快捷按键~!
// @author 张仨
// @match *://*/*
// @connect api.gmit.vip
// @grant unsafeWindow
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_xmlhttpRequest
// @grant GM_registerMenuCommand
// @require https://cdn.jsdelivr.net/npm/zhangsan-layui@1.0.4/layui.min.js
// ==/UserScript==
function newPromise() {
let load;
new Promise((resolve, reject) => {
layer.prompt({
formType: 2,
value: '',
title: '拼音转换,输入中文后点击转换',
btn: ['转换', '取消'],
btn2: function () {
reject("取消转换");
},
cancel: function () {
reject("关闭了窗口");
}
}, function (value, index) {
if (/^[\u4E00-\u9FA5\s]+$/.test(value)) {
resolve(value);
load = layer.load(2);
layer.close(index);
} else {
layer.msg(`你输入的是${value},请确认输入值不包含除中文和空格之外的字符`, { icon: 5 });
}
});
}).then(value => {
new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: "GET",
responseType: "json",
url: `https://api.gmit.vip/Api/Pinyin?format=json&text=${value}`,
onload: function (xhr) {
layer.close(load);
if (xhr.readyState === 4 && xhr.status === 200) {
if (xhr.response.code == 200) {
resolve(xhr.response.data.pinyin);
} else {
reject("转换失败!");
}
} else {
reject("服务器无响应或请求出错!");
}
}
})
}).then(res => {
layer.confirm(res, {
icon: 1,
title: '转换成功!',
btn: ['复制拼音', '不了,谢谢!',]
}, function (index) {
navigator.clipboard.writeText(res);
layer.msg('复制成功!', { icon: 1 });
layer.close(index);
}, function (index) {
layer.close(index);
});
}).catch(err => {
layer.confirm("请确认输入值不为纯空格字符或非法字符,若一直失败,请稍后再试或联系作者维护!", {
icon: 5,
title: err,
btn: ['联系作者', '重新输入',]
}, function (index) {
window.open('https://bbs.tampermonkey.net.cn/thread-2193-1-1.html');
layer.close(index);
}, function (index) {
newPromise();
layer.close(index);
});
})
}).catch(err => {
layer.msg(err);
})
};
GM_registerMenuCommand("🗝️设置快捷键", function () {
layer.prompt({
formType: 0,
value: 'x',
title: '输入一个小写字母作为快捷按键',
}, function (value, index) {
if (/^[a-z]+$/.test(value) && value.length == 1) {
GM_setValue("key", value)
layer.msg(`设置成功,快捷按键为:Ctrl+${value},刷新网页后生效!`, { icon: 1 });
layer.close(index);
} else {
layer.msg(`你输入的是${value},请确认输入值为单个小写字母`, { icon: 5 });
}
});
}, "H");
let key = GM_getValue("key") || "x";
unsafeWindow.onkeydown = function (e) {
if (e.ctrlKey && e.key == key) {
newPromise();
}
};