Use Tampermonkey requests for Suno WAV checks

This commit is contained in:
DESKTOP-KSVGT20\shkim
2026-05-02 00:28:50 +09:00
parent 73cd8cea8f
commit 248f4a21d4
+77 -15
View File
@@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name ChatGPT to Suno Prompt Copier // @name ChatGPT to Suno Prompt Copier
// @namespace local.suno-helper // @namespace local.suno-helper
// @version 0.7.6 // @version 0.7.7
// @description Copy structured ChatGPT song prompt versions into Suno fields. // @description Copy structured ChatGPT song prompt versions into Suno fields.
// @author local // @author local
// @match https://chatgpt.com/* // @match https://chatgpt.com/*
@@ -19,6 +19,7 @@
// @grant GM_addStyle // @grant GM_addStyle
// @grant GM_getValue // @grant GM_getValue
// @grant GM_download // @grant GM_download
// @grant GM_xmlhttpRequest
// @grant GM_setClipboard // @grant GM_setClipboard
// @grant GM_setValue // @grant GM_setValue
// @grant unsafeWindow // @grant unsafeWindow
@@ -27,7 +28,7 @@
(function () { (function () {
"use strict"; "use strict";
const SCRIPT_VERSION = "0.7.6"; const SCRIPT_VERSION = "0.7.7";
const STORAGE_KEY = "chatgptSunoPromptSlots"; const STORAGE_KEY = "chatgptSunoPromptSlots";
const SLOT_COUNT = 2; const SLOT_COUNT = 2;
const isChatGPT = location.hostname.includes("chatgpt.com") || location.hostname.includes("chat.openai.com"); const isChatGPT = location.hostname.includes("chatgpt.com") || location.hostname.includes("chat.openai.com");
@@ -817,20 +818,10 @@
} }
async function callSunoJson(url, options = {}) { async function callSunoJson(url, options = {}) {
const page = typeof unsafeWindow !== "undefined" ? unsafeWindow : window; const response = await callSunoText(url, options);
const response = await page.fetch(url, { const finalWavUrl = extractWavUrl(response.finalUrl || url);
credentials: "include",
...options,
headers: {
"accept": "application/json, text/plain, */*",
...(options.method && options.method !== "GET" ? { "content-type": "application/json" } : {}),
...(options.headers || {}),
},
});
const finalUrl = response.url || url;
const finalWavUrl = extractWavUrl(finalUrl);
if (response.ok && finalWavUrl) return finalWavUrl; if (response.ok && finalWavUrl) return finalWavUrl;
const text = await response.text(); const text = response.text || "";
const data = tryParseJson(text) || text; const data = tryParseJson(text) || text;
rememberSunoResponse(url, data); rememberSunoResponse(url, data);
if (!response.ok) { if (!response.ok) {
@@ -839,6 +830,66 @@
return data; return data;
} }
async function callSunoText(url, options = {}) {
const page = typeof unsafeWindow !== "undefined" ? unsafeWindow : window;
const headers = {
"accept": "application/json, text/plain, */*",
...(options.method && options.method !== "GET" && options.method !== "HEAD" ? { "content-type": "application/json" } : {}),
...(options.headers || {}),
};
try {
const response = await page.fetch(url, {
credentials: "include",
...options,
headers,
});
const finalUrl = response.url || url;
if (response.ok && extractWavUrl(finalUrl)) {
return {
ok: true,
status: response.status,
statusText: response.statusText,
finalUrl,
text: "",
};
}
return {
ok: response.ok,
status: response.status,
statusText: response.statusText,
finalUrl,
text: await response.text(),
};
} catch (fetchError) {
if (typeof GM_xmlhttpRequest !== "function") throw fetchError;
return callTampermonkeyRequest(url, { ...options, headers });
}
}
function callTampermonkeyRequest(url, options = {}) {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: options.method || "GET",
url,
headers: options.headers || {},
data: options.body,
anonymous: false,
timeout: 45000,
onload: (response) => {
resolve({
ok: response.status >= 200 && response.status < 300,
status: response.status,
statusText: response.statusText || "",
finalUrl: response.finalUrl || url,
text: response.responseText || "",
});
},
onerror: (error) => reject(new Error(error?.error || "GM_xmlhttpRequest failed")),
ontimeout: () => reject(new Error("GM_xmlhttpRequest timeout")),
});
});
}
function extractWavUrl(value) { function extractWavUrl(value) {
if (!value) return ""; if (!value) return "";
if (typeof value === "string") { if (typeof value === "string") {
@@ -900,6 +951,17 @@
} catch (error) { } catch (error) {
console.warn("[ChatGPT to Suno] URL probe failed", url, error); console.warn("[ChatGPT to Suno] URL probe failed", url, error);
} }
if (typeof GM_xmlhttpRequest === "function") {
try {
const response = await callTampermonkeyRequest(url, {
...options,
headers: options.headers || {},
});
if (response.ok || response.status === 206) return true;
} catch (error) {
console.warn("[ChatGPT to Suno] GM URL probe failed", url, error);
}
}
} }
return false; return false;
} }