function downloadBlob(blob, name, url) {
const anchor = document.createElement("a");
anchor.setAttribute("download", name || "");
const blobUrl = URL.createObjectURL(blob);
anchor.href = blobUrl + (url ? ("#" + url) : "");
anchor.click();
setTimeout(() => URL.revokeObjectURL(blobUrl), 5000);
}
blob:https://imgur.com/11fb6df9-e45b-4acf-b3eb-60d5d4656747#https://i.imgur.com/X92aA5Y.jpeg
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function download(blob, name, url) {
const anchor = document.createElement("a");
anchor.setAttribute("download", name || "");
const blobUrl = URL.createObjectURL(blob);
anchor.href = blobUrl + (url ? ("#" + url) : "");
anchor.click();
setTimeout(() => URL.revokeObjectURL(blobUrl), 5000);
}
url
(третий параметр) к blobUrl
, может потом пригодиться — посмотреть, откуда (по какой именно ссылке) файл был скачен.blob:https://imgur.com/11fb6df9-e45b-4acf-b3eb-60d5d4656747#https://i.imgur.com/X92aA5Y.jpeg
for (const url of urls) {
const resp = await fetch(url);
const blob = await resp.blob();
const name = new URL(url).pathname.match(/[^\/]*$/)[0];
download(blob, name, url);
await sleep(200);
}
// ==UserScript==
// @name Toster Example
// @match https://qna.habr.com/*
// @match https://toster.ru/*
// @grant GM.xmlHttpRequest
// @connect https://example.com
// ==/UserScript==
!async function() {
console.log("userscript start");
const response = await new Promise((resolve, reject) => {
GM.xmlHttpRequest({
method: "get",
url: "https://example.com",
responseType: "text",
onload: resolve,
onerror: reject,
});
});
console.log("response:", response);
const {response: text} = response;
console.log("text:", text);
}();
@match
), и успешно логирует в консоль содержание https://example.com несмотря на CORS. chrome.storage.local.get(console.log);
chrome.storage.sync.get(console.log);
function searchInFileExplorer(search, location) {
window.open(`search-ms:displayname=${search}&crumb=System.Generic.String:${search}&crumb=location:${location}`);
}
searchInFileExplorer("Search Text", "C:\\Users\\");
"C:%HOMEPATH%\\Downloads"
("\\" т.к. экраннирование)