This commit introduces a new UserScript for Binance Alpha Farm, designed to enhance user experience by providing a welcome popup and utility functions for managing input fields. The script includes features for creating and updating popups, as well as handling user interactions effectively.
151 lines
5.1 KiB
JavaScript
151 lines
5.1 KiB
JavaScript
// ==UserScript==
|
|
// @name Binance Alpha Farm Agent
|
|
// @namespace http://baf.thuanle.me
|
|
// @version 2025.07.30
|
|
// @author TL
|
|
// @match https://www.binance.com/en/alpha/bsc/*
|
|
// @run-at document-idle
|
|
// @grant GM_setValue
|
|
// @grant GM_getValue
|
|
// ==/UserScript==
|
|
|
|
(async () => {
|
|
'use strict';
|
|
|
|
// Utility functions
|
|
const TL = {
|
|
log: (msg, ...args) => console.log(`[TL] ${msg}`, ...args),
|
|
delay: ms => new Promise(resolve => setTimeout(resolve, ms)),
|
|
getEl: sel => document.querySelector(sel),
|
|
waitForEl: async (sel, timeout = 10000) => {
|
|
const start = Date.now();
|
|
while (Date.now() - start < timeout) {
|
|
const el = document.querySelector(sel);
|
|
if (el) return el;
|
|
await TL.delay(100);
|
|
}
|
|
throw new Error(`waitForEl: Timeout waiting for ${sel}`);
|
|
},
|
|
|
|
eventFillFloatInputById(id, value) {
|
|
const input = document.getElementById(id);
|
|
if (!input) return;
|
|
const current = parseFloat(input.value);
|
|
const target = parseFloat(value);
|
|
if (Math.abs(current - target) < 1e-8) return;
|
|
|
|
const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value').set;
|
|
setter.call(input, value);
|
|
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
}
|
|
};
|
|
|
|
// POP UP HELPER
|
|
const PopupHelper = {
|
|
defaultStyle: `
|
|
position: fixed;
|
|
bottom: 20px;
|
|
background: white;
|
|
color: #000;
|
|
border: 1px solid #ccc;
|
|
padding: 12px 16px;
|
|
border-radius: 6px;
|
|
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
|
|
z-index: 999999;
|
|
font-family: sans-serif;
|
|
font-size: 12px;
|
|
display: block !important;
|
|
visibility: visible !important;
|
|
opacity: 1 !important;
|
|
`,
|
|
|
|
createOrUpdatePopup(id, options = {}) {
|
|
try {
|
|
const {
|
|
html = '',
|
|
position = 'bottom-right',
|
|
customStyle = '',
|
|
onClick = null
|
|
} = options;
|
|
|
|
TL.log(`🔍 Creating/updating popup with ID: ${id}`);
|
|
|
|
let popup = document.getElementById(id);
|
|
if (!popup) {
|
|
popup = document.createElement('div');
|
|
popup.id = id;
|
|
document.body.appendChild(popup);
|
|
TL.log(`🔍 Created new popup element with ID: ${id}`);
|
|
} else {
|
|
TL.log(`🔍 Found existing popup with ID: ${id}`);
|
|
}
|
|
|
|
// Set position based on position parameter
|
|
let positionStyle = '';
|
|
switch (position) {
|
|
case 'bottom-right':
|
|
positionStyle = 'right: 20px;';
|
|
break;
|
|
case 'bottom-left':
|
|
positionStyle = 'left: 20px;';
|
|
break;
|
|
case 'top-right':
|
|
positionStyle = 'top: 20px; right: 20px;';
|
|
break;
|
|
case 'top-left':
|
|
positionStyle = 'top: 20px; left: 20px;';
|
|
break;
|
|
case 'center':
|
|
positionStyle = 'top: 50%; left: 50%; transform: translate(-50%, -50%);';
|
|
break;
|
|
default:
|
|
positionStyle = 'right: 20px;';
|
|
}
|
|
|
|
// Apply styles
|
|
const finalStyle = this.defaultStyle + positionStyle + customStyle;
|
|
popup.style.cssText = finalStyle;
|
|
TL.log(`🔍 Applied styles to popup: ${finalStyle.substring(0, 100)}...`);
|
|
|
|
// Set content
|
|
popup.innerHTML = html;
|
|
TL.log(`🔍 Set HTML content to popup (length: ${html.length})`);
|
|
|
|
// Add click handler if provided
|
|
if (onClick && typeof onClick === 'function') {
|
|
popup.onclick = onClick;
|
|
}
|
|
|
|
TL.log(`✅ Popup ${id} created/updated successfully`);
|
|
return popup;
|
|
} catch (error) {
|
|
TL.log(`❌ Error in createOrUpdatePopup: ${error.message}`);
|
|
return null;
|
|
}
|
|
},
|
|
|
|
removePopup(id) {
|
|
const popup = document.getElementById(id);
|
|
if (popup) {
|
|
popup.remove();
|
|
}
|
|
},
|
|
|
|
|
|
};
|
|
|
|
// Main script logic
|
|
// show welcome popup
|
|
PopupHelper.createOrUpdatePopup('welcome-popup', {
|
|
html: `
|
|
<div>
|
|
<h2>Welcome to Binance Alpha Farm Agent</h2>
|
|
<p>This script will help you manage your Binance Alpha Farm tasks more efficiently.</p>
|
|
<p>Click <a href="https://baf.thuanle.me" target="_blank">here</a> to learn more.</p>
|
|
<button id="close-welcome-popup">Close</button>
|
|
</div>
|
|
`, onClick: () => {
|
|
PopupHelper.removePopup('welcome-popup');
|
|
}
|
|
});
|
|
})(); |