Skip to content

Instantly share code, notes, and snippets.

@Hunlongyu
Last active January 15, 2024 01:55
Show Gist options
  • Save Hunlongyu/683db53d2320a68f41c6141c2dc5d32a to your computer and use it in GitHub Desktop.
Save Hunlongyu/683db53d2320a68f41c6141c2dc5d32a to your computer and use it in GitHub Desktop.
『净网卫士』Coze bot
// ==UserScript==
// @name 『净网卫士』Coze - Free GPT4
// @name:zh 『净网卫士』Coze - Free GPT4
// @namespace https://github.com/Hunlongyu
// @icon https://i.loli.net/2019/04/22/5cbd720718fdb.png
// @version 0.8.2
// @description 隐藏左侧和中间面板,增大右侧输入面板
// @description:zh 隐藏左侧和中间面板,增大右侧输入面板
// @author Hunlongyu
// @match https://www.coze.com/*
// @grant GM_addStyle
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_notification
// @run-at document-end
// @downloadURL https://update.greasyfork.org/scripts/484336/%E3%80%8E%E5%87%80%E7%BD%91%E5%8D%AB%E5%A3%AB%E3%80%8FCoze%20-%20free%20GPT4.user.js
// @updateURL https://update.greasyfork.org/scripts/484336/%E3%80%8E%E5%87%80%E7%BD%91%E5%8D%AB%E5%A3%AB%E3%80%8FCoze%20-%20free%20GPT4.meta.js
// ==/UserScript==
(function() {
'use strict';
let css_expand = `
html{
min-width: 600px !important;
}
body{
min-width: 600px !important;
}
.sidesheet-container{
grid-template-columns: 0 0 1fr !important;
}
.nXeOwsObZZQToAlSP5Kx{
display: none !important;
}
`;
let css_shrink = `
.sidesheet-container{
grid-template-columns: 13fr 13fr 14fr !important;
}
.nXeOwsObZZQToAlSP5Kx{
display: "";
}
`;
let button_expand = `
<button id="button_expand" style="margin-left: 10px;" class="semi-button semi-button-primary" type="button" aria-disabled="false"><span class="semi-button-content" x-semi-prop="children">展开</span></button>
`;
let button_shrink = `
<button id="button_shrink" style="margin-left: 10px; display: none;" class="semi-button semi-button-primary" type="button" aria-disabled="false"><span class="semi-button-content" x-semi-prop="children">恢复</span></button>
`;
// 执行网页净化功能
const task = () => {
let parent_flag = false;
let textarea_flag = false;
let timer = setInterval(() => {
const parent = document.querySelector(".semi-spin-children");
if (parent) {
const first_child = parent.children[0];
if (first_child && !parent_flag) {
first_child.insertAdjacentHTML("beforeend", button_expand);
first_child.insertAdjacentHTML("beforeend", button_shrink);
toggleHandle();
parent_flag = true;
}
}
const textareas = document.querySelectorAll(".semi-input-textarea-wrapper");
if (textareas) {
const lastTextarea = textareas[textareas.length - 1];
if (lastTextarea && !textarea_flag) {
const style = window.getComputedStyle(lastTextarea);
const maxHeight = style.getPropertyValue("max-height");
if (maxHeight == "78px") {
lastTextarea.style.maxHeight = "300px";
}
textarea_flag = true;
}
}
if (parent_flag && textarea_flag) {
if (!GM_getValue('is_expand')) {
GM_setValue('is_expand', false);
} else {
GM_addStyle(css_expand);
document.querySelector("#button_expand").style.display = "none";
document.querySelector("#button_shrink").style.display = "";
}
clearInterval(timer);
}
}, 500);
};
// 展开 / 恢复 点击事件
const toggleHandle = () => {
const btn_expand = document.querySelector("#button_expand");
btn_expand.addEventListener('click', () => {
GM_addStyle(css_expand);
document.querySelector("#button_expand").style.display = "none";
document.querySelector("#button_shrink").style.display = "";
});
const btn_shrink = document.querySelector("#button_shrink");
btn_shrink.addEventListener('click', () => {
GM_addStyle(css_shrink);
document.querySelector("#button_expand").style.display = "";
document.querySelector("#button_shrink").style.display = "none";
});
};
// 脚本菜单注册 / 及其交互
let menu_expand_id = null;
const registerMenuCommand = () => {
if (menu_expand_id) GM_unregisterMenuCommand(menu_expand_id);
menu_expand_id = GM_registerMenuCommand(`${GM_getValue('is_expand')?'✅':'❌'}` + `${GM_getValue('is_expand')?'已开启自动展开功能(点击关闭)':'已关闭自动展开功能(点击打开)'}`, () => {
if (GM_getValue('is_expand') == true) {
GM_setValue('is_expand', false);
GM_notification({ text: `已关闭自动展开功能`, timeout: 3500, onclick: function() { location.reload(); } });
} else {
GM_setValue('is_expand', true);
GM_notification({ text: `已开启自动展开功能`, timeout: 3500, onclick: function() { location.reload(); } });
}
registerMenuCommand();
});
}
registerMenuCommand();
// 用来完善 SPA 单页应用 url 变化但脚本不生效的问题
function registerEventHandler(target) {
return function registerTargetEventHandler(methodName) {
const originMethod = target[methodName]
return function eventHandler(...args) {
const event = new Event(methodName.toLowerCase())
originMethod.apply(target, args)
window.dispatchEvent(event)
return originMethod
}
}
}
const registerHistoryEventHandler = registerEventHandler(window.history)
window.history.pushState = registerHistoryEventHandler("pushState")
window.history.replaceState = registerHistoryEventHandler("replaceState")
function todo() {
const url = window.location.href;
const bot_reg = /^https:\/\/www\.coze\.com\/.*\/bot\/.*$/;
if (bot_reg.test(url)) {
task();
}
const explore_reg = /^https:\/\/www\.coze\.com\/explore\/.*$/;
if (explore_reg.test(url)) {
task();
}
}
window.addEventListener("pushstate", todo, false)
todo();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment