- получить случайную категорию, например. «https://www.britanica.com/sitemap/c»
- запросите, сколько ссылок есть в этой категории, и выберите одну из них, например. "https://www.britanica.com/sitemap/c/190"
- На этой странице получите ссылку на статью и перенаправьте ее туда, например. «https://www.britannica.com/topic/The-Co ... orecasting» (это первая ссылка в моем примере)
(1) букмарклет (2) скрипт tampermonkey (3) html-страницу.
По какой-то причине ничего из этого это сработало. По словам чатгпта, это безопасность браузера.
Мне интересно, действительно ли существуют какие-то ограничения для этого? Каков рекомендуемый способ сделать это (т. е. я хотел бы запустить код JavaScript, который вычисляет случайную страницу из Britannica и перенаправляет туда)? Можно ли это сделать с помощью javascript или есть ли здесь проблемы с безопасностью?
Обновление: вот код, который не работал (отказ от ответственности: я сгенерировал его с помощью grok):
(1) букмарклет
Ниже приведен код, который действительно работал, проблема в том, что количество статей жестко закодировано (т. е. когда я сохраняю это как букмарклет и открываю его, он действительно перенаправляет меня на статью)
Код: Выделить всё
javascript:(async function(){const cats=['0-9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];const weights=[20,80,60,70,50,60,40,40,50,40,20,30,40,50,40,50,50,15,50,90,60,30,20,20,10,10,10];let total=weights.reduce((a,b)=>a+b,0);let r=Math.random()*total;let sum=0;let cat;for(let i=0;i parseInt(a.getAttribute('href').match(/\/(\\d+)$/)[1])).filter(n => !isNaN(n));
const maxPage = Math.max(...pageNums);
if (maxPage < 1) { window.location.href = 'https://www.britannica.com'; return; }
const randomPage = Math.floor(Math.random() * maxPage) + 1;
const listUrl = \`https://www.britannica.com/sitemap/\${cat}/\${randomPage}\`;
GM_xmlhttpRequest({
method: "GET",
url: listUrl,
onload: function(resp) {
if (resp.status !== 200) { window.location.href = 'https://www.britannica.com'; return; }
const doc2 = parser.parseFromString(resp.responseText, 'text/html');
const links = Array.from(doc2.querySelectorAll('a[href^="/"]')).filter(a => {
const h = a.getAttribute('href');
return h && (
/^\/(biography|topic|animal|plant|sports|technology|art|geography|place|science|money|video|story|gallery)\//.test(h) ||
/\/[^\/]+-\d+$/.test(h)
);
});
if (links.length === 0) { window.location.href = 'https://www.britannica.com'; return; }
const randomLink = links[Math.floor(Math.random() * links.length)];
window.location.href = 'https://www.britannica.com' + randomLink.getAttribute('href');
}
});
}
});
}.toString()})();`;document.body.appendChild(script);script.remove();})();(2)
Для tampermonkey сценарий ниже, казалось, делал то, что я хочу, но просто делал это в непрерывном цикле без остановки.
Код: Выделить всё
// ==UserScript==
// @name Random Britannica Launcher
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Menu launcher for the main random Britannica script
// @author You
// @match *://*/*
// @grant GM_registerMenuCommand
// ==/UserScript==
(function() {
'use strict';
GM_registerMenuCommand("Random Britannica Article", function() {
// Paste the entire main script code here as a string and eval it
eval(`const cats = ['0-9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
const cat = cats[Math.floor(Math.random() * cats.length)];
const indexUrl = \`https://www.britannica.com/sitemap/\${cat}\`;
GM_xmlhttpRequest({
method: "GET",
url: indexUrl,
onload: function(response) {
if (response.status !== 200) { window.location.href = 'https://www.britannica.com'; return; }
const parser = new DOMParser();
const doc = parser.parseFromString(response.responseText, 'text/html');
const pageLinks = Array.from(doc.querySelectorAll(\`a[href^="/sitemap/\${cat}/"]\`));
if (pageLinks.length === 0) { window.location.href = 'https://www.britannica.com'; return; }
const pageNums = pageLinks.map(a => parseInt(a.getAttribute('href').match(/\/(\\\\d+)$/)[1])).filter(n => !isNaN(n));
const maxPage = Math.max(...pageNums);
if (maxPage < 1) { window.location.href = 'https://www.britannica.com'; return; }
const randomPage = Math.floor(Math.random() * maxPage) + 1;
const listUrl = \`https://www.britannica.com/sitemap/\${cat}/\${randomPage}\`;
GM_xmlhttpRequest({
method: "GET",
url: listUrl,
onload: function(resp) {
if (resp.status !== 200) { window.location.href = 'https://www.britannica.com'; return; }
const doc2 = parser.parseFromString(resp.responseText, 'text/html');
const links = Array.from(doc2.querySelectorAll('a[href^="/"]')).filter(a => {
const h = a.getAttribute('href');
return h && (
/^\/(biography|topic|animal|plant|sports|technology|art|geography|place|science|money|video|story|gallery)\//.test(h) ||
/\/[^\/]+-\\d+$/.test(h)
);
});
if (links.length === 0) { window.location.href = 'https://www.britannica.com'; return; }
const randomLink = links[Math.floor(Math.random() * links.length)];
window.location.href = 'https://www.britannica.com' + randomLink.getAttribute('href');
}
});
}
});`);
});
})();Затем еще одна версия, которую я вообще не смог запустить (в том смысле, что я даже не был уверен, как заставить tampermonkey ее запустить)
Код: Выделить всё
// ==UserScript==
// @name Random Britannica Article (Perfect Fixed)
// @namespace http://tampermonkey.net/
// @version 4.0
// @description Menu-only one-click true random Britannica article - dynamic, no loops, works every time
// @author You + Grok
// @match *://*/*
// @grant GM_xmlhttpRequest
// @grant GM_registerMenuCommand
// ==/UserScript==
GM_registerMenuCommand("Get Random Britannica Article", function() {
'use strict';
const cats = ['0-9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
const cat = cats[Math.floor(Math.random() * cats.length)];
const indexUrl = `https://www.britannica.com/sitemap/${cat}`;
GM_xmlhttpRequest({
method: "GET",
url: indexUrl,
onload: function(response) {
if (response.status !== 200) {
window.location.href = 'https://www.britannica.com';
return;
}
const text = response.responseText;
// Regex to catch all (/sitemap/cat/number) paths
const pageMatches = [...text.matchAll(/\/sitemap\/[^\/]+\/(\d+)/g)];
if (pageMatches.length === 0) {
window.location.href = 'https://www.britannica.com';
return;
}
const pageNums = pageMatches.map(m => parseInt(m[1]));
const maxPage = Math.max(...pageNums);
const randomPage = Math.floor(Math.random() * maxPage) + 1;
const listUrl = `https://www.britannica.com/sitemap/${cat}/${randomPage}`;
GM_xmlhttpRequest({
method: "GET",
url: listUrl,
onload: function(resp) {
if (resp.status !== 200) {
window.location.href = 'https://www.britannica.com';
return;
}
const parser = new DOMParser();
const doc2 = parser.parseFromString(resp.responseText, 'text/html');
const links = Array.from(doc2.querySelectorAll('a[href^="/"]')).filter(a => {
const h = a.getAttribute('href');
return h && (
/^\/(biography|topic|animal|plant|sports|technology|art|geography|place|science|money|video|story|gallery)\//.test(h) ||
/\/[^\/]+-\d+$/.test(h)
);
});
if (links.length === 0) {
window.location.href = 'https://www.britannica.com';
return;
}
const randomLink = links[Math.floor(Math.random() * links.length)];
window.location.href = 'https://www.britannica.com' + randomLink.getAttribute('href');
},
onerror: function() {
window.location.href = 'https://www.britannica.com';
}
});
},
onerror: function() {
window.location.href = 'https://www.britannica.com';
}
});
});(3) Для HTML я сохранил локальный файл .html и открыл его в Chrome. Он просто перенаправил меня на ту же страницу britannica.com
Код: Выделить всё
Random Britannica
(async function() {
const cats = ['0-9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
const cat = cats[Math.floor(Math.random() * cats.length)];
const indexUrl = `https://www.britannica.com/sitemap/${cat}`;
try {
const resp = await fetch(indexUrl);
if (!resp.ok) throw '';
const text = await resp.text();
// Extract all page numbers from raw text (handles their Markdown links)
const matches = [...text.matchAll(/\/sitemap\/[^\/]+\/(\d+)/g)];
if (matches.length === 0) throw '';
const pageNums = matches.map(m => parseInt(m[1]));
const maxPage = Math.max(...pageNums);
const randomPage = Math.floor(Math.random() * maxPage) + 1;
const listUrl = `https://www.britannica.com/sitemap/${cat}/${randomPage}`;
const resp2 = await fetch(listUrl);
if (!resp2.ok) throw '';
const text2 = await resp2.text();
const parser = new DOMParser();
const doc = parser.parseFromString(text2, 'text/html');
const links = Array.from(doc.querySelectorAll('a[href^="/"]')).filter(a => {
const h = a.getAttribute('href');
return h && (
/^\/(biography|topic|animal|plant|sports|technology|art|geography|place|science|money|video|story|gallery)\//.test(h) ||
/\/[^\/]+-\d+$/.test(h)
);
});
if (links.length === 0) throw '';
const randomLink = links[Math.floor(Math.random() * links.length)];
window.location.href = 'https://www.britannica.com' + randomLink.getAttribute('href');
} catch (e) {
// Rare failure fallback
window.location.href = 'https://www.britannica.com';
}
})();
Loading your random Britannica article...
Заранее извиняюсь, если это очевидно для экспертов, как я уже упоминал, я имел в виду, что это скорее мета-вопрос относительно ограничений инструментов, которые я пытаюсь здесь использовать.
Подробнее здесь: https://stackoverflow.com/questions/798 ... ca-article
Мобильная версия