Post Action

将当前页面的网址发送到指定的服务器,服务器地址信息需要再代码中修改以适合自己使用!

// ==UserScript==
// @name         Post Action
// @namespace    https://github.com/RANSAA
// @version      0.0.6
// @description  将当前页面的网址发送到指定的服务器,服务器地址信息需要再代码中修改以适合自己使用!
// @author       sayaDev
// @license      MIT License
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAACCFJREFUeF7tW3lUVFUY/10YlkMupAyRDApailaKnYpKD5kHNayO4o7lsWNlekgCt1ySx1CGLSc0l5O0eyTNFkitbLXytJiN0mJQJqLMmMlo4IIyMNy6d3jPmWGW95gZg57fXzPvfff7vvu7937fd+/7LkGAKUaIj7dSazIITaJAIoB4AElu1O4DcJgAFaCkrJE07q7VH6sKpIkkEMK1eXEplGIsQNNg67QPRMoB7CAEpTV51V/5IMhlU78BcPmjvbsGh1lmgmCG7512280KULxibQgt+vupyjp/gOEzAJFCfGQIrAsp6GJ/GCRXBgEpaETw07X6qlq5bVzx+QSANlf3CCXIAxDpixE+tK0lFHk1+cZVbZXRJgC0QmwSBSkEMKytiv3c7gsCmlOjN5UplasYgKjc2IdAyAtKFV0UfkpnmfNN65XoUgRAlBBXCNBsJQouPi9ZadZX58jVKxuAKEH3BoAMuYL/Y75NZr1xqhwbZAGgFXTvUiBdjsD2wkOAkhq9cZw3e7wC0MFG3rm/XmeCRwA6xpr3OsYefYJbANq1t/fWZ+f3HqKDSwBa4jzbmPxviIAOdpUnuAQgStDtbEdJjr8G4Quz3ni7s7BWALSktyv9pbU9ySEU2c5pswMAbGOjQdOh/zC3DzRetU3QJNhvoBwA0ApxT17sXV2ge9x6ypOCGn31EvG5BADfz4dbfNpaXuzOtFWf9XxopHieIAEQlatbAIKn2yq0Q7WjWGjONz7DbL4AgKBjR08+Hl91GBgqzHpjfwkA2xke/bLDmO8HQwkht7EzRj4DooS45wAqewvpB/3tQAQpNOur54oA/ApQPiVURHwZkEghJl4DDYv9X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TNEHyAHgz9wqaIKCXS4tTZAGf+ba/JESANjpr7uSFS5MDHEsFPVZcY3LDo3ql4qNGa/yd7H5fWDKPch/L/9sBVbuWuvQhoW9qUmTYTDtw4QNUxUBULn4V3QO6wy23Hou7+sgd25KFhYPX6AEgH2yEqHZtzyI/FG5XPCWH99BZonj99FOYZ1QsaAMYZowyTDReYlTV7SU7Sd+mrvHARwlM0AYsRQPD5nF26e9NBY/GA0SCJWLy9E5rJMSAEplpcJM4vdZu5DQzZYEsXhbd95WoRIZHsnXpEjTNz+ADyo+gv2MYCHzgPkP6LrGShHEfgSVAMD0iOCy3yyMnqg/gf7RiTxaiCRnCRCWCsvdDDHBokd2tQaYR1+wfYnDZigjaRJWjXnWASDWlmWDqUWjpfzh45nbMbjHILjyAUUT1iD92jGSE2TtIzTh+Hm+oVU4NtaZwNJyFiVkAcA2Q6yMrcl2FC6bUnoPRerVw9E9ohv2GA08B2Apqzsa1OM63HfDNM6zwVAsK4uUYwzLR9hmjNEzOwthabbIaSbxNKEpoeVARFXngS0AkHKzvnrApSMxBofqD0UZCFFqPhbnAKj9w4jqP41xX6Dmj6M8q1P753E+C2z1v+oskBBTJFWXyNh8AS+GVm+RlC0stuOiaEUZPwClZXIXlkJHKI72hobn4ulLpbLe8LP5BBUXS9tFBvWWy3csnxCgCxMSCO05OgT6yowIgqovTdk7TdVem7MHQdUXJ+2BUO3VWVd5xIXL07jDD+V3FQD5sN1fnnaXULEyvBAaYn99nhUZuSvJYTc/q8Tr88EkePcxfVVAr8//A++41FP10osLAAAAAElFTkSuQmCC

// @match        http://*/*
// @match        *://*/*

// @require 	https://update.greasyfork.org/scripts/494214/1432041/TKBaseSDK.js

// @grant       unsafeWindow
// @grant       GM_openInTab
// @grant       GM_xmlhttpRequest


// @connect     self        //添加需要跨域不弹出用户选择页面的标注
// @connect     localhost
// @connect     127.0.0.1
// @connect     *


// @compatible   chrome
// @compatible   edge
// @compatible   firefox
// @compatible   opera
// @compatible   safari


// @noframes
// ==/UserScript==
/**
 * 目前支持的参数:
 * url: 当前页面网址
 * title:当前页面标题
 * four_color_av_m3u8:四色AV中的m3u8视频地址
 **/




/**
 * 配置信息,可更具需求更改配置
 **/
const SERVER_CONFIG = {
    host: "127.0.0.1",          //服务器地址
    port: "80",                 //服务器端口,直接使用”“不指定端口
    method: "POST",             //请求方式
    scheme: "http",             //协议类型
    taskAdd: "task/add",     //yt-dlp 任务添加API Query Path路劲地址
};



/**
 * 将参数组装成JSON格式
 * 获取当前需要的所有属性
 * url:当前页面的url地址
 * title:当前网页的标题
 * four_color_av_m3u8:四色AV中播放页面中的m3u8链接--值为可选
 **/
function loadParameterJSON(){
    // //示例
    // var json = {
    //  jsonrpc:'2.0',
    //  method:'aria2.addUri',
    //  id:url,
    //  params:[
    //   [url],
    //  ]
    // }
    // return JSON.stringify(json)


    //当前网页地址
    let url = window.location.href;
    //当前网页标题
    let title = document.title;
    //四色AV中的m3u8链接
    let four_color_av_m3u8 =  getFourColorAVM3u8Url();

    let json = {
        "url": url,
        "title": title,
        "four_color_av_m3u8":four_color_av_m3u8
    };
    return JSON.stringify(json)
}



(function() {
    'use strict';
    TKBaseSDK.initToast();
    addSendServerButton();
})();




// -------------------------------Setup UI-------------------------------
function addSendServerButton(){
    //添加style
    TKBaseSDK.addButtonStyle();

    //创建Send URL按钮
    let sendURL = TKBaseSDK.createListItemButton("Send URL");
    //定义的是事件被触发后要做的事情
    sendURL.addEventListener("click", function() {
        sendLocationURL();
    });

    //创建Copy URL按钮
    let copyURL = TKBaseSDK.createListItemButton("Copy URL");
    //定义的是事件被触发后要做的事情
    copyURL.addEventListener("click", function() {
        copyLocationURL();
    });

    //创建Copy M3u8按钮
    let copyM3u8 = TKBaseSDK.createListItemButton("Copy M3U8");
    //定义的是事件被触发后要做的事情
    copyM3u8.addEventListener("click", function() {
        copyM3u8Link();
    });

    //创建Copy yt-dlp-n按钮
    let copyYtDlpN = TKBaseSDK.createListItemButton("Copy yt-dlp-n");
    //定义的是事件被触发后要做的事情
    copyYtDlpN.addEventListener("click", function() {
        copyYtDlpM3u8Link();
    });

    //创建Copy yt-dlp按钮
    let copyYtDlp = TKBaseSDK.createListItemButton("Copy yt-dlp");
    //定义的是事件被触发后要做的事情
    copyYtDlp.addEventListener("click", function() {
        copyYtDlpLocationLink();
    });


    //创建Copy Magnet按钮
    let copyMagnet = TKBaseSDK.createListItemButton("Copy Magnet");
    copyMagnet.addEventListener("click", function() {
        copyMagnetLinks();
    });



    let list = document.createElement("TKButtonList");
    list.className = "TKButtonListStyle";
    list.appendChild(sendURL);
    //list.appendChild(copyURL);
    list.appendChild(copyM3u8);
    list.appendChild(copyYtDlpN);
    list.appendChild(copyYtDlp);
    list.appendChild(copyMagnet);
    document.body.appendChild(list);
}

// -------------------------------Setup UI-------------------------------



// -------------------------------Action-------------------------------

/**
 * 发送数据当前页面的URL到指定服务器
 */
function sendLocationURL()
{
    console.log(`Send Server`);

    //服务器的地址
    var serverUrl = SERVER_CONFIG.scheme + "://" + SERVER_CONFIG.host + ":" + SERVER_CONFIG.port + "/" + SERVER_CONFIG.taskAdd;
    if (SERVER_CONFIG.port === "" ) {
        serverUrl = SERVER_CONFIG.scheme + "://" + SERVER_CONFIG.host +  "/" + SERVER_CONFIG.taskAdd;
    }


    //发送的参数数据
    let data = loadParameterJSON();
    console.log(`serverUrl: ${serverUrl}`);
    console.log(`parameter: ${data}`);


    //GM_xmlhttpRequest方式请求
    GM_xmlhttpRequest({
        method: SERVER_CONFIG.method,
        url: serverUrl,
        headers: {
            'Content-Type': 'application/json; charset=utf-8',
        },
        data: data,
        onload: function(response) {
            console.log(response);
            console.log(`readyState:${response.readyState}`);
            console.log(`status:${response.status}`);
            console.log(`statusText:${response.statusText}`);
            console.log(`responseHeaders:\n${response.responseHeaders}`);
            console.log(`responseText:${response.responseText}`);
            if (response.status === 200) {
                console.log(`Send URL Success: ${url}`);
                TKBaseSDK.showToast("当前URL地址发送成功!",1);
            } else {
                console.log(`Send URL Error: ${url} statusText: ${response.statusText}`);
                TKBaseSDK.showToast("当前URL地址发送失败!",0);
            }
        },
        onerror: function(response) {
            // 请求发生错误时执行
            console.error("Request failed:", response);
            let msg = `发送失败,Send Server服务地址:${response.finalUrl}`;
            console.log(msg);
            TKBaseSDK.showToastWtihTime(msg, 0, 4000);
        }
    });
}

/**
 * 拷贝当前网页地址
 **/
function copyLocationURL(){
    //当前网页地址
    let url = window.location.href;
	TKBaseSDK.copyToClipBoard(url);
    TKBaseSDK.showToast("复制成功!",1);
}


/**
 * 获取并拷贝m3u8链接
 **/
function copyM3u8Link(){
    let m3u8Url = getFourColorAVM3u8Url();
    if (m3u8Url === "") {
        TKBaseSDK.showToast("复制失败,没有找到M3U8链接!",0);
        TKBaseSDK.copyToClipBoard("");
    }else{
        TKBaseSDK.showToast("复制成功!",1);
        //拷贝到剪切板
        TKBaseSDK.copyToClipBoard(m3u8Url);
    }
}

/**
 * 获取四色AV中的m3u8链接地址
 **/
function getFourColorAVM3u8Url(){
    // Define a regular expression to match the playUrl variable pattern
    const playUrlPattern = /var playUrl\s*=\s*"([^"]+)"/;

    // Get all script tags on the page
    const scripts = Array.from(document.getElementsByTagName('script'));

    // Look for the script containing the playUrl variable
    const matchedScript = scripts.find(script => playUrlPattern.test(script.textContent));

    var m3u8Url = ""

    // If found, extract the URL
    if (matchedScript) {
    const match = matchedScript.textContent.match(playUrlPattern);
    m3u8Url = match && match[1];
    console.log('四色AV站点:m3u8 URL:', m3u8Url);
    } else {
    console.warn('四色AV站点:m3u8 URL could not be found.');
    }

    return m3u8Url
}



/**
 * 获取四色AV中视频的标题
 **/
function getFourColorAVTitle(){
    var title = ""

    // 获取包含 h1 元素的 div.main 元素
    const mainDiv = document.querySelector('.wrap > .main');
    // 确认 main 元素存在
    if (mainDiv) {
        // 在 mainDiv 内查找 h1 元素
        const h1Element = mainDiv.querySelector('h1');

        // 确认 h1 元素存在
        if (h1Element) {
            // 获取 h1 元素的文本内容
            const h1Content = h1Element.textContent.trim();
            console.log('四色AV站点:查找h1 内容为:', h1Content);
             title = h1Content
        } else {
            console.warn('四色AV站点:查找视频标题时未找到 h1 元素!');
            // title = "test---222"
        }
    } else {
        console.warn('四色AV站点:查找视频标题时未找到 .main 元素!');
        // title = "test-333"
    }

    return title
}


/**
 * 拷贝四色AV中的标题与m3u8链接,并且组装yt-dlp-n命令
 * 格式: yt-dlp-n "视频标题" "m3u8视频地址"
 **/
function copyYtDlpM3u8Link(){
    // let m3u8Url = getFourColorAVM3u8Url();
    // let title = getFourColorAVTitle();
    let m3u8Url = filterVideoLink();
    let title = filterVideoTitle();

    if (m3u8Url === "" || title === "") {
        TKBaseSDK.showToast("yt-dlp-n下载命令复制失败,没有找到视频链接!",0);
        TKBaseSDK.copyToClipBoard("");
    }else{
        TKBaseSDK.showToast("yt-dlp-n下载命令复制成功!",1);

        //拷贝到剪切板
        let cmd = "yt-dlp-n " + "\"" + title + "\"" + " \"" + m3u8Url + "\"" + "\n\n";
        TKBaseSDK.copyToClipBoard(cmd);
    }
}


/**
 * 过滤获取当前视频指定的名称
 **/ 
function filterVideoTitle(){
    //默认获取四色AV中视频的标题
    var title = getFourColorAVTitle();
    //获取当前站点的域名
    let hostname = document.location.hostname
    //当前网站路径
    let pathname = document.location.pathname

    //所有直接以网页标题作为title的域名集合
    const titleSet = new Set([
        "beeg.com",
    ]);
    if (pathname != "/") { //不是网站的主页才可以获取视频的标题
        if ( titleSet.has(hostname) ) {
            title = document.title;
        }
    }


    // 处理文件名中的特殊字符
    title = sanitizeFileName(title);

    return title
}


/**
 * 过滤获取视频的指定链接
 **/ 
function filterVideoLink(){
    //默认获取四色AV中视频的m3u8链接
    var videoURL = getFourColorAVM3u8Url();
    if (videoURL === "") {
        TKBaseSDK.showToast("没有找到M3U8链接,直接使用当前网页地址作为视频链接!",0);
        videoURL = document.location.href;
    }
    return videoURL
}





/**
 * 拷贝当前网页链接, 并组装yt-dlp命令
 * 格式:yt-dlp "网页地址"
 **/
function copyYtDlpLocationLink(){
    let url = window.location.href;

    TKBaseSDK.showToast("yt-dlp下载命令复制成功!",1);
    //拷贝到剪切板
    let cmd = "yt-dlp " + "\"" + url + "\"" + "\n\n";
    TKBaseSDK.copyToClipBoard(cmd);

}



/**
 * 功能:拷贝当前页面的所有magnet磁力链接
 **/
function copyMagnetLinks(){
    const magnetLinks = getMagnetLinks();
    // 转换成换行分隔的字符串
    const magnetLinksString = magnetLinks.join('\n');
    if (magnetLinksString === "" ) {
        TKBaseSDK.showToast("Copy Mangnet失败,没有找到磁力链接!",0);
        TKBaseSDK.copyToClipBoard("");
    }else{
        TKBaseSDK.showToast("Copy Mangnet成功!",1);
        //拷贝到剪切板
        TKBaseSDK.copyToClipBoard(magnetLinksString);
    }
}


/**
 * 获取页面中的所有magnet磁力链接
 **/ 
function getMagnetLinks(){
    // 从整个 HTML 文档中提取磁力链接
    const htmlContent = document.documentElement.innerHTML;
    const magnetRegex = /magnet:\?xt=urn:[a-zA-Z0-9:]+/g;
    const magnetLinks = Array.from(new Set(htmlContent.match(magnetRegex) || []));
    console.log(magnetLinks);
    return magnetLinks;
}



/**
 * 功能:将字符串中不能作为文件名的字符替换成"_",并且如果存在英文的双引号"替换成英文的单引号'
 * replace(/"/g, '“');      //正则表达式
 * replaceAll('"', '“');    //直接替换函数
 **/
function sanitizeFileName(filename) {
    //使用正则表达式将"替换成'
    var name = filename;
    name = name.replace(/"/g, "'");     //注意:这儿使用的正则表达式无法将"替换成',然后再调用这个函数的外部直接对字符串操作又可以,不知道是什么原因。所以直接使用replaceAll方法进行替换。
    name = name.replaceAll('"', "'");
    name = name.replaceAll(':', ":");
    name = name.replaceAll('”', "'");

    //将不能作为文件名的字符替换为:_
    name = name.replace(/[:]+/g, '_');  // -osx
    // name = name.replace(/[\/\\:*?"<>|]+/g, '_');  // win

    return name
}



/**
 *  功能:将字符串中不能作为路径的字符替换成”_“
 **/ 
function sanitizePath(path) {
  return path.replace(/[\/\\:*?"<>|]+/g, '_');  // 替换为单个下划线
}



// -------------------------------Action-------------------------------