Создание случайной британской статьи [закрыто]Javascript

Форум по Javascript
Ответить
Anonymous
 Создание случайной британской статьи [закрыто]

Сообщение Anonymous »

Я пытался получить работающий код JavaScript, который генерировал бы случайную статью с сайта britannica.com. Вот логика, которую я хотел бы реализовать: Я пробовал:
(1) букмарклет (2) скрипт tampermonkey (3) html-страницу.
Однако я мог бы заставить ни один из трех вышеперечисленных подходов не работать. Мой главный вопрос: какой подход будет наиболее адекватным для того, что я пытаюсь сделать? Существуют ли какие-либо ограничения с точки зрения безопасности для тех, которые я упомянул, которые помешали бы мне делать то, что я пытаюсь сделать?
Ниже я прилагаю код и некоторые комментарии для трех упомянутых мной подходов (отказ от ответственности: я сгенерировал код с помощью 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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Javascript»