2023-08-03 17:25:37 +03:00
|
|
|
// ==UserScript==
|
|
|
|
// @name Quarkly Upload Image via URL
|
2023-08-03 19:52:37 +03:00
|
|
|
// @namespace https://github.com/Maks1mS/userscripts
|
2023-08-04 14:22:29 +03:00
|
|
|
// @version 0.3
|
2023-08-03 17:25:37 +03:00
|
|
|
// @description try to take over the world!
|
|
|
|
// @author Maxim Slipenko
|
2023-08-04 14:22:29 +03:00
|
|
|
// @match https://quarkly.io/*
|
2023-08-03 17:25:37 +03:00
|
|
|
// @icon https://www.google.com/s2/favicons?sz=64&domain=quarkly.io
|
|
|
|
// @grant GM_xmlhttpRequest
|
|
|
|
// @grant GM_addStyle
|
2023-08-04 14:22:29 +03:00
|
|
|
// @grant window.onurlchange
|
2023-08-03 17:25:37 +03:00
|
|
|
// ==/UserScript==
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const QUARKLY_UPLOAD_BUTTON_ID = 'upload-image-button';
|
|
|
|
const UPLOAD_BY_URL_ID = 'upload-image-button-by-url';
|
2023-08-04 14:22:29 +03:00
|
|
|
const QUARKLY_PROJECT_URL_PREFIX = 'https://quarkly.io/project/'
|
2023-08-03 17:25:37 +03:00
|
|
|
|
|
|
|
function insertAfter(newNode, existingNode) {
|
|
|
|
existingNode.parentNode.insertBefore(newNode, existingNode.nextSibling);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onClick() {
|
|
|
|
let url = prompt("URL");
|
|
|
|
let text;
|
|
|
|
if (url == null || url == "") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GM.xmlHttpRequest({
|
|
|
|
method: "GET",
|
|
|
|
url,
|
|
|
|
responseType: "blob",
|
|
|
|
onload: function(r) {
|
|
|
|
const finalUrl = r.finalUrl;
|
|
|
|
// TODO: Better filename detection
|
|
|
|
const filename = url.substring(url.lastIndexOf('/')+1);
|
2023-08-03 19:48:21 +03:00
|
|
|
var arr = r.responseHeaders.split('\r\n');
|
|
|
|
var headers = arr.reduce(function (acc, current, i){
|
|
|
|
var parts = current.split(': ');
|
|
|
|
acc[parts[0]] = parts[1];
|
|
|
|
return acc;
|
|
|
|
}, {});
|
2023-08-03 17:25:37 +03:00
|
|
|
const file = new File(
|
|
|
|
[r.response],
|
|
|
|
filename,
|
|
|
|
{
|
2023-08-03 19:48:21 +03:00
|
|
|
type: headers['content-type'],
|
2023-08-03 17:25:37 +03:00
|
|
|
lastModified:new Date().getTime()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const uploadButton = document.getElementById(QUARKLY_UPLOAD_BUTTON_ID);
|
|
|
|
if (!uploadButton) {
|
|
|
|
alert("Upload button not found!")
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const container = new DataTransfer();
|
|
|
|
container.items.add(file);
|
|
|
|
uploadButton.files = container.files;
|
|
|
|
uploadButton.dispatchEvent(new Event('change', { bubbles: true }));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function createUploadButton() {
|
|
|
|
const button = document.createElement("button");
|
|
|
|
button.innerText = "Upload via URL";
|
|
|
|
button.id = UPLOAD_BY_URL_ID;
|
|
|
|
button.onclick = onClick;
|
|
|
|
return button
|
|
|
|
}
|
|
|
|
|
2023-08-04 14:22:29 +03:00
|
|
|
// TODO: better popup detection
|
|
|
|
|
|
|
|
let timer;
|
|
|
|
|
2023-08-03 17:25:37 +03:00
|
|
|
function run() {
|
|
|
|
const dest = document.querySelector(`label[for="${QUARKLY_UPLOAD_BUTTON_ID}"]`);
|
|
|
|
if (dest) {
|
|
|
|
if (document.getElementById(UPLOAD_BY_URL_ID)) return;
|
|
|
|
const button = createUploadButton();
|
|
|
|
insertAfter(button, dest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-04 14:22:29 +03:00
|
|
|
if (window.onurlchange === null) {
|
|
|
|
window.addEventListener('urlchange', (info) => {
|
|
|
|
if (info?.url?.startsWith(QUARKLY_PROJECT_URL_PREFIX)) {
|
|
|
|
timer = setInterval(run, 3000);
|
|
|
|
} else {
|
|
|
|
clearInterval(timer);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-08-03 17:25:37 +03:00
|
|
|
|
|
|
|
var css = `
|
|
|
|
#${UPLOAD_BY_URL_ID} {
|
|
|
|
color: var(--white);
|
|
|
|
background: var(--interface-primary-base);
|
|
|
|
border-radius: var(--interface-controls-radius);
|
|
|
|
padding: 6px 12px;
|
|
|
|
line-height: 20px;
|
|
|
|
font-size: 14px;
|
|
|
|
font-weight: 600;
|
|
|
|
border: 0;
|
|
|
|
outline: none;
|
|
|
|
cursor: pointer;
|
|
|
|
display: inline-flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
text-decoration: none;
|
|
|
|
margin-left: 20px;
|
|
|
|
height: 44px;
|
|
|
|
}
|
|
|
|
#${UPLOAD_BY_URL_ID}:hover {
|
|
|
|
background: var(--interface-primary-l1);
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
GM_addStyle(css);
|
|
|
|
})();
|