Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GM_xmlhttpRequest redirect 设置 manual 会一直卡住 #330

Open
wuhua111 opened this issue Dec 15, 2024 · 1 comment
Open

GM_xmlhttpRequest redirect 设置 manual 会一直卡住 #330

wuhua111 opened this issue Dec 15, 2024 · 1 comment
Labels
兼容问题 其它管理器可以运行,脚本猫不能运行

Comments

@wuhua111
Copy link

wuhua111 commented Dec 15, 2024

如果设置超时,会抛出超时错误。同样的脚本,我也测试了篡改猴,脚本在那里时可以正常获取到信息的。
这时测试url https://civitai.com/models/833294/noobai-xl-nai-xl
下面时脚本 用来复制模型的下载链接的

// ==UserScript==
// @name         civitai
// @namespace    http://tampermonkey.net/
// @version      0.1
// @author       hua
// @match        https://civitai.com/*
// @grant        unsafeWindow
// @grant        GM_xmlhttpRequest
// @run-at       document-start
// @license      MIT
// ==/UserScript==



(function() {
    'use strict';
    const href = location.href;
    if (href.startsWith('https://civitai.com/models')) {
        setModelCopyButton()
    }


    function setModelCopyButton() { 
    let Interval = null;
    function addCopyButtons() {
            const buttons = document.querySelectorAll('a[href^="/api/download/models/"][type="button"]');
            if (buttons.length === 0) {
                return;
            }
            clearInterval(Interval);
            buttons.forEach(button => {
                const wrapper = document.createElement('span');
                wrapper.style.cssText = 'display: inline-flex; align-items: center;';
                
                button.parentNode.insertBefore(wrapper, button);
                wrapper.appendChild(button);
                
                const buttonStyle = window.getComputedStyle(button);
                
                const copyButton = document.createElement('button');
                copyButton.textContent = 'copy';
                copyButton.style.cssText = `
                    margin-left: 4px;
                    padding: 2px 6px;
                    font-size: 12px;
                    border: 1px solid #ccc;
                    border-radius: 3px;
                    cursor: pointer;
                    height: ${buttonStyle.height};
                    background-color: ${buttonStyle.backgroundColor};
                    color: ${buttonStyle.color};
                    width: 70px;
                    text-align: center;
                `;
                
                copyButton.addEventListener('click', function() {
                    const uri = button.getAttribute('href');
                    copyButton.textContent = 'loading...';
                    
                    GM_xmlhttpRequest({
                        method: "GET",
                        url: `https://civitai.com${uri}`,
                        timeout: 10000,
                        anonymous: false,
                        redirect: 'manual',
                        onload: function(response) {
                        
                            
                            const downloadUrl = response.responseHeaders.match(/location:(.*?)(?:\r?\n)/i)?.[1];
                            if (downloadUrl) {
                                navigator.clipboard.writeText(downloadUrl).then(() => {
                                    copyButton.textContent = 'copied!';
                                    setTimeout(() => {
                                        copyButton.textContent = 'copy';
                                    }, 2000);
                                });
                            } else {
                                copyButton.textContent = 'failed';
                                setTimeout(() => {
                                    copyButton.textContent = 'copy';
                                }, 2000);
                            }
                        },
                        onerror: function(error) {
                            console.error('Error:', error);
                            copyButton.textContent = 'error';
                            setTimeout(() => {
                                copyButton.textContent = 'copy';
                            }, 2000);
                        },
                        ontimeout: function() {
                            copyButton.textContent = 'timeout';
                            setTimeout(() => {
                                copyButton.textContent = 'copy';
                            }, 2000);
                        }
                    });
                });
                
                wrapper.appendChild(copyButton);
            });
    }

    Interval = setInterval(addCopyButtons, 500);
    }
})();
@CodFrm CodFrm added the 兼容问题 其它管理器可以运行,脚本猫不能运行 label Dec 15, 2024
@CodFrm
Copy link
Member

CodFrm commented Dec 15, 2024

脚本猫还没实现redirect,好像也是篡改猴最近才加入的 https://www.tampermonkey.net/documentation.php#api:GM_xmlhttpRequest

脚本猫使用了:maxRedirects https://docs.scriptcat.org/docs/dev/api/#gm_xmlhttprequest-

@CodFrm CodFrm added this to the Manifest V3之后计划 milestone Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
兼容问题 其它管理器可以运行,脚本猫不能运行
Projects
None yet
Development

No branches or pull requests

2 participants