// 合并两个脚本的功能 (function() { 'use strict'; // 激活状态变量 let isActivated = false; // 替换状态变量…
- User
// 合并两个脚本的功能 (function() { 'use strict';
code - Assistant
// 合并两个脚本的功能 (function() { 'use strict';
// 激活状态变量
let isActivated = false;
// 替换状态变量
let isReplacingFlash = false;
// 激活函数,需要输入密码
function activateScript(password) {
const correctPassword = "705878475"; // 设置您的密码
if (password === correctPassword) {
isActivated = true;
GM_setValue("isActivated", true);
alert("脚本已激活!");
} else {
alert("密码错误!");
}
}
// 切换Flash替换功能
function toggleReplaceFlash() {
isReplacingFlash = !isReplacingFlash;
GM_setValue("isReplacingFlash", isReplacingFlash);
alert(isReplacingFlash ? "Flash替换功能已开启!" : "Flash替换功能已关闭!");
}
// 检查激活状态
function checkActivation() {
if (!isActivated && !GM_getValue("isActivated", false)) {
const password = prompt("请输入密码以激活脚本:", "");
if (password) {
activateScript(password);
}
}
}
// 注册菜单命令
function registerMenuCommands() {
GM_unregisterMenuCommand("点我注入");
GM_registerMenuCommand("点我注入", () => {
checkActivation();
if (isActivated || GM_getValue("isActivated", false)) {
// 刷新当前页面
location.href = location.href;
alert("页面已刷新,脚本功能已尝试执行!");
} else {
alert("脚本未激活,请先激活!");
}
});
GM_registerMenuCommand("切换Flash替换功能", toggleReplaceFlash);
}
// 替换Flash内容的函数
const replaceFlashWithRuffle = (element) => {
const src = element.getAttribute('src');
if (src && src.endsWith('.swf')) {
const container = element.parentNode;
const width = element.getAttribute('width') || '100%';
const height = element.getAttribute('height') || '100%';
// Remove the old Flash element
element.parentNode.removeChild(element);
// Add the Ruffle player
const ruffleDiv = document.createElement('div');
ruffleDiv.setAttribute('data-ruffle', 'https://unpkg.com/@ruffle-rs/ruffle');
ruffleDiv.style.width = width;
ruffleDiv.style.height = height;
container.appendChild(ruffleDiv);
}
};
// 替换Flash内容
const replaceAllFlashContent = () => {
const elementsToCheck = ['object', 'embed', 'iframe'];
elementsToCheck.forEach(tagName => {
const elements = document.getElementsByTagName(tagName);
for (let i = 0; i < elements.length; i++) {
replaceFlashWithRuffle(elements[i]);
}
});
};
// 监听DOM变化
const observer = new MutationObserver(() => {
if (isReplacingFlash) {
replaceAllFlashContent();
}
});
// 配置观察者
const config = { childList: true, subtree: true };
// 开始监听body的变化
observer.observe(document.body, config);
// 初始化
registerMenuCommands();
// 加载Ruffle脚本
const ruffleScriptSrc = 'https://unpkg.com/@ruffle-rs/ruffle';
const script = document.createElement('script');
script.src = ruffleScriptSrc;
script.onload = () => {
console.log('Ruffle script loaded.');
if (isReplacingFlash || GM_getValue("isReplacingFlash", false)) {
replaceAllFlashContent();
}
};
script.onerror = () => {
console.error('Failed to load Ruffle script.');
};
document.head.appendChild(script);
})();
To merge the functionalities of two scripts, you need to ensure that all intended operations are combined in a cohesive way without conflicts. Here's how you can combine the features from the existing script into a single script effectively:
(function() {
'use strict';
// State variables
let isActivated = false;
let isReplacingFlash = false;
// Set your password here
const correctPassword = "705878475";
// Activation function
function activateScript(password) {
if (password === correctPassword) {
isActivated = true;
GM_setValue("isActivated", true);
alert("Script is activated!");
} else {
alert("Incorrect password!");
}
}
// Toggle function for Flash replacement
function toggleReplaceFlash() {
isReplacingFlash = !isReplacingFlash;
GM_setValue("isReplacingFlash", isReplacingFlash);
alert(isReplacingFlash ? "Flash replacement enabled!" : "Flash replacement disabled!");
}
// Check activation status
function checkActivation() {
if (!isActivated && !GM_getValue("isActivated", false)) {
const password = prompt("Enter password to activate script:", "");
if (password) {
activateScript(password);
}
}
}
// Register menu commands
function registerMenuCommands() {
// Unregister previous command to prevent duplication
GM_unregisterMenuCommand("Inject");
GM_registerMenuCommand("Inject", () => {
checkActivation();
if (isActivated || GM_getValue("isActivated", false)) {
// Refresh the current page
location.href = location.href;
alert("Page refreshed, trying to execute script functionality!");
} else {
alert("Script not activated, please activate first!");
}
});
GM_registerMenuCommand("Toggle Flash Replacement", toggleReplaceFlash);
}
// Function to replace Flash content
const replaceFlashWithRuffle = (element) => {
const src = element.getAttribute('src');
if (src && src.endsWith('.swf')) {
const container = element.parentNode;
const width = element.getAttribute('width') || '100%';
const height = element.getAttribute('height') || '100%';
// Remove the old Flash element
element.parentNode.removeChild(element);
// Add the Ruffle player
const ruffleDiv = document.createElement('div');
ruffleDiv.setAttribute('data-ruffle', 'https://unpkg.com/@ruffle-rs/ruffle');
ruffleDiv.style.width = width;
ruffleDiv.style.height = height;
container.appendChild(ruffleDiv);
}
};
// Replace all Flash content
const replaceAllFlashContent = () => {
const elementsToCheck = ['object', 'embed', 'iframe'];
elementsToCheck.forEach(tagName => {
const elements = document.getElementsByTagName(tagName);
for (let i = 0; i < elements.length; i++) {
replaceFlashWithRuffle(elements[i]);
}
});
};
// Monitor DOM changes
const observer = new MutationObserver(() => {
if (isReplacingFlash) {
replaceAllFlashContent();
}
});
// Observer configuration
const config = { childList: true, subtree: true };
// Start observing changes in the document body
observer.observe(document.body, config);
// Initialize the script
registerMenuCommands();
// Load Ruffle script for Flash replacement
const ruffleScriptSrc = 'https://unpkg.com/@ruffle-rs/ruffle';
const script = document.createElement('script');
script.src = ruffleScriptSrc;
script.onload = () => {
console.log('Ruffle script loaded.');
if (isReplacingFlash || GM_getValue("isReplacingFlash", false)) {
replaceAllFlashContent();
}
};
script.onerror = () => {
console.error('Failed to load Ruffle script.');
};
document.head.appendChild(script);
})();
Unified State Management: The use of GM_setValue and GM_getValue helps in persisting the state across page reloads or script restarts.
Modular Functions: Functions such as activateScript, toggleReplaceFlash, and replaceFlashWithRuffle break the script into logical components, enhancing readability and maintenance.
DOM Monitoring: By leveraging a MutationObserver, the script efficiently tracks changes in the DOM to replace Flash content dynamically.
User Interaction: Through GM_registerMenuCommand, users can interact with the script to activate and toggle Flash replacement functionality.
Loading External Scripts: Carefully managing the loading of the Ruffle script ensures that Flash replacement is ready to proceed once the Ruffle library is available.
Not yet included in a published release.