|
// ==UserScript== |
|
// @name CrowdinScript |
|
// @namespace CrowdinScript |
|
// @version 0.2 |
|
// @description try to take over the world! (By Arth) |
|
// @author Arth(https://github.com/4i8) |
|
// @match https://crowdin.com/translate/* |
|
// @match https://*.crowdin.com/* |
|
// @icon https://www.google.com/s2/favicons?sz=64&domain=crowdin.com |
|
// @grant none |
|
// ==/UserScript== |
|
|
|
(function () { |
|
"use strict"; |
|
//By Arth |
|
/** |
|
* @variable |
|
* @constants |
|
*/ |
|
const Prompt_Copy = ""; // keep empty to disable GPT prompt |
|
const Prompt_CopyAll = ""; // keep empty to disable GPT prompt for copy all |
|
const EventListeners = ["next_page", "prev_page", "click", "keydown"]; |
|
const Elements_CopyAll = [ |
|
`<li> |
|
<button |
|
style="color:#ffffff;display: flex;flex-direction: row;font-size: 11px;padding: 6px 8px 3px 8px;background-color:#DA5E5A;" |
|
class="btn btn-secondary" |
|
> |
|
Copy All |
|
</button> |
|
</li>`, |
|
`<li> |
|
<button |
|
style="color:#ffffff;display: flex;flex-direction: row;font-size: 11px;padding: 6px 8px 3px 8px; background-color:#201737;" |
|
class="btn btn-secondary" |
|
> |
|
Copy All |
|
</button> |
|
</li> |
|
`, |
|
]; |
|
/** |
|
* @function |
|
*/ |
|
function addElement(htmlContent) { |
|
const dummyContainer = document.createElement("div"); |
|
dummyContainer.innerHTML = htmlContent; |
|
return dummyContainer.firstChild; |
|
} |
|
async function copyContent(text) { |
|
await navigator.clipboard.writeText(text); |
|
} |
|
function waitForLoading() { |
|
return new Promise(function (resolve) { |
|
const I = setInterval(function () { |
|
let d = document.getElementById("loading_block").style.display; |
|
if (d == "none") { |
|
clearInterval(I); |
|
resolve(true); |
|
d = null; |
|
} |
|
}, 1000); |
|
}); |
|
} |
|
function addCopyButtons(prompt) { |
|
document.querySelectorAll(".untranslated_phrase").forEach((i, index) => { |
|
var status = false; |
|
if (i instanceof Node) { |
|
for (const childNode of i.childNodes) { |
|
if (childNode.nodeName === "BUTTON") { |
|
status = true; |
|
break; |
|
} |
|
} |
|
} |
|
if (status) return; |
|
if (i?.childNodes[1]?.className === "btn btn-primary") return; |
|
let text = !prompt |
|
? i.textContent.replace(/Copied!$|Copy$/, "") |
|
: `"${i.textContent.replace(/Copied!$|Copy$/, "")}"\n${prompt}`; |
|
i.appendChild( |
|
addElement( |
|
`<button style="margin-top: 7px; display: flex; flex-direction: row;" class="btn btn-primary">Copy</button>` |
|
) |
|
).addEventListener("click", (a) => { |
|
navigator.permissions |
|
.query({ name: "clipboard-write" }) |
|
.then(async (result) => { |
|
if (result.state == "granted" || result.state == "prompt") { |
|
a.target.textContent = "Copied!"; |
|
a.target.style.backgroundColor = "#198754"; |
|
setTimeout(() => { |
|
a.target.textContent = "Copy"; |
|
a.target.disabled = false; |
|
a.target.style.backgroundColor = null; |
|
a.target.classList = "btn btn-primary"; |
|
}, 1000); |
|
await copyContent(text); |
|
} else { |
|
alert("You need to grant permission to copy the text"); |
|
} |
|
}); |
|
}); |
|
}); |
|
} |
|
/** |
|
* @main |
|
*/ |
|
waitForLoading().then(() => { |
|
addCopyButtons(Prompt_Copy); |
|
Elements_CopyAll.forEach((i, index) => { |
|
document |
|
.getElementById("editor-menu") |
|
.appendChild(addElement(i)) |
|
.addEventListener("click", (a) => { |
|
navigator.permissions |
|
.query({ name: "clipboard-write" }) |
|
.then(async (result) => { |
|
if (result.state == "granted" || result.state == "prompt") { |
|
setTimeout(() => { |
|
a.target.textContent = "Copy All"; |
|
a.target.disabled = false; |
|
a.target.style.backgroundColor = |
|
index === 0 ? "#DA5E5A" : "#201737"; |
|
a.target.classList = "btn btn-secondary"; |
|
}, 1000); |
|
if (index === 0) { |
|
let items = [ |
|
...document.querySelectorAll(".untranslated_phrase"), |
|
]; |
|
if (!items?.length) return; |
|
a.target.textContent = "Copied!"; |
|
a.target.style.backgroundColor = "#198754"; |
|
$.jGrowl("Copy All UnTranslated Strings", { |
|
life: 1400, |
|
}); |
|
await copyContent( |
|
items |
|
.map( |
|
(i, index) => |
|
`${index + 1}-` + |
|
i.textContent.replace(/Copied!$|Copy$/, "") |
|
) |
|
.join("\n") + `\n${Prompt_CopyAll}` |
|
); |
|
} else { |
|
let items = [...document.querySelectorAll(".source-string")]; |
|
if (!items?.length) return; |
|
a.target.textContent = "Copied!"; |
|
a.target.style.backgroundColor = "#198754"; |
|
$.jGrowl("Copy All Source Strings", { |
|
life: 1400, |
|
}); |
|
await copyContent( |
|
items |
|
.map( |
|
(i, index) => |
|
`${index + 1}-` + |
|
i.textContent.replace(/Copied!$|Copy$/, "") |
|
) |
|
.join("\n") + `\n${Prompt_CopyAll}` |
|
); |
|
} |
|
} else { |
|
alert("You need to grant permission to copy UnTranslated text"); |
|
} |
|
}); |
|
}); |
|
}); |
|
}); |
|
EventListeners.forEach((i) => { |
|
document.addEventListener(i, async () => { |
|
if (i === "keydown") { |
|
waitForLoading().then(() => { |
|
addCopyButtons(Prompt_Copy); |
|
}); |
|
} else { |
|
waitForLoading().then(() => { |
|
addCopyButtons(Prompt_Copy); |
|
document.querySelectorAll(".translated_phrase").forEach((i) => { |
|
var status = false; |
|
if (i instanceof Node) { |
|
for (const childNode of i.childNodes) { |
|
if (childNode.nodeName === "BUTTON") { |
|
status = true; |
|
break; |
|
} |
|
} |
|
if (!status) return; |
|
i.removeChild(i.children[0]); |
|
} |
|
}); |
|
}); |
|
} |
|
}); |
|
}); |
|
})(); |