feat(popularity) add ranking by nb# of comments
This commit is contained in:
parent
4cfa80e9fc
commit
b33b45de94
@ -1,8 +1,8 @@
|
|||||||
// ==UserScript==
|
// ==UserScript==
|
||||||
// @name Hacker News URL Highlighter with Enhanced Panel and Scores
|
// @name Hacker News URL Highlighter with Enhanced Panel and Scores & Comments
|
||||||
// @namespace https://news.ycombinator.com/
|
// @namespace https://news.ycombinator.com/
|
||||||
// @version 1.5
|
// @version 1.6
|
||||||
// @description Highlights URLs in comments on Hacker News, lists them in a toggleable panel with sorting, color coding based on frequency or upvotes, and displays scores next to each URL. Provides export functionality and hover previews only in comments.
|
// @description Highlights URLs in comments, displays them in a toggleable panel with sorting (frequency, name, upvotes, comment count).
|
||||||
// @author MorganGeek
|
// @author MorganGeek
|
||||||
// @match https://news.ycombinator.com/item?id=*
|
// @match https://news.ycombinator.com/item?id=*
|
||||||
// @grant GM_xmlhttpRequest
|
// @grant GM_xmlhttpRequest
|
||||||
@ -11,267 +11,175 @@
|
|||||||
// @run-at document-end
|
// @run-at document-end
|
||||||
// ==/UserScript==
|
// ==/UserScript==
|
||||||
|
|
||||||
(function () {
|
(function() {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// Configuration des couleurs en fonction des fréquences ou des upvotes
|
const COLORS={low:'#d1e7dd',medium:'#fff3cd',high:'#f8d7da'};
|
||||||
const COLORS = {
|
const toggleButton=document.createElement('button');
|
||||||
low: '#d1e7dd', // Vert pâle
|
toggleButton.textContent='Afficher les URLs';
|
||||||
medium: '#fff3cd', // Jaune pâle
|
Object.assign(toggleButton.style,{
|
||||||
high: '#f8d7da' // Rouge pâle
|
position:'fixed',bottom:'10px',right:'10px',zIndex:1000,padding:'10px 15px',
|
||||||
};
|
backgroundColor:'#ff6600',color:'#fff',border:'none',borderRadius:'5px',cursor:'pointer'
|
||||||
|
});
|
||||||
// Créer un bouton pour basculer l'affichage du panneau
|
|
||||||
const toggleButton = document.createElement('button');
|
|
||||||
toggleButton.textContent = 'Afficher les URLs';
|
|
||||||
toggleButton.style.position = 'fixed';
|
|
||||||
toggleButton.style.bottom = '10px';
|
|
||||||
toggleButton.style.right = '10px';
|
|
||||||
toggleButton.style.zIndex = 1000;
|
|
||||||
toggleButton.style.padding = '10px 15px';
|
|
||||||
toggleButton.style.backgroundColor = '#ff6600';
|
|
||||||
toggleButton.style.color = '#fff';
|
|
||||||
toggleButton.style.border = 'none';
|
|
||||||
toggleButton.style.borderRadius = '5px';
|
|
||||||
toggleButton.style.cursor = 'pointer';
|
|
||||||
document.body.appendChild(toggleButton);
|
document.body.appendChild(toggleButton);
|
||||||
|
|
||||||
// Créer un panneau flottant pour afficher les URLs (caché par défaut)
|
const panel=document.createElement('div');
|
||||||
const panel = document.createElement('div');
|
panel.id='url-panel';
|
||||||
panel.id = 'url-panel';
|
Object.assign(panel.style,{
|
||||||
panel.style.position = 'fixed';
|
position:'fixed',top:'10px',right:'10px',width:'350px',backgroundColor:'#fff',
|
||||||
panel.style.top = '10px';
|
border:'1px solid #ccc',borderRadius:'5px',padding:'10px',boxShadow:'0 0 10px rgba(0,0,0,.2)',
|
||||||
panel.style.right = '10px';
|
maxHeight:'80vh',overflowY:'auto',zIndex:1000,display:'none'
|
||||||
panel.style.width = '350px';
|
});
|
||||||
panel.style.backgroundColor = '#fff';
|
panel.innerHTML=`
|
||||||
panel.style.border = '1px solid #ccc';
|
|
||||||
panel.style.borderRadius = '5px';
|
|
||||||
panel.style.padding = '10px';
|
|
||||||
panel.style.boxShadow = '0px 0px 10px rgba(0,0,0,0.2)';
|
|
||||||
panel.style.maxHeight = '80vh';
|
|
||||||
panel.style.overflowY = 'auto';
|
|
||||||
panel.style.zIndex = 1000;
|
|
||||||
panel.style.display = 'none'; // Caché par défaut
|
|
||||||
panel.innerHTML = `
|
|
||||||
<h4>URLs</h4>
|
<h4>URLs</h4>
|
||||||
<div style="margin-bottom: 10px;">
|
<div style="margin-bottom:10px;">
|
||||||
<label for="sort-select">Trier par:</label>
|
<label for="sort-select">Trier par:</label>
|
||||||
<select id="sort-select" style="width: 100%; padding: 5px; margin-top: 5px;">
|
<select id="sort-select" style="width:100%;padding:5px;margin-top:5px;">
|
||||||
<option value="frequency">Fréquence</option>
|
<option value="frequency">Fréquence</option>
|
||||||
<option value="name">Nom</option>
|
<option value="name">Nom</option>
|
||||||
<option value="upvotes">Upvotes</option>
|
<option value="upvotes">Upvotes</option>
|
||||||
|
<option value="commentaires">Commentaires</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<ul id="url-list" style="margin: 0; padding: 0; list-style: none;"></ul>
|
<ul id="url-list" style="margin:0;padding:0;list-style:none;"></ul>
|
||||||
<div style="margin-top: 10px;">
|
<div style="margin-top:10px;">
|
||||||
<label for="limit-select">Afficher:</label>
|
<label for="limit-select">Afficher:</label>
|
||||||
<select id="limit-select" style="width: 100%; padding: 5px; margin-top: 5px;">
|
<select id="limit-select" style="width:100%;padding:5px;margin-top:5px;">
|
||||||
<option value="all">Tout</option>
|
<option value="all">Tout</option>
|
||||||
<option value="10">Top 10</option>
|
<option value="10">Top 10</option>
|
||||||
<option value="20">Top 20</option>
|
<option value="20">Top 20</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<button id="copy-urls" style="margin-top: 10px; display: block; width: 100%; padding: 10px; background-color: #ff6600; color: #fff; border: none; border-radius: 5px; cursor: pointer;">Copier les URLs</button>
|
<button id="copy-urls" style="margin-top:10px;display:block;width:100%;padding:10px;background:#ff6600;color:#fff;border:none;border-radius:5px;cursor:pointer;">
|
||||||
|
Copier les URLs
|
||||||
|
</button>
|
||||||
`;
|
`;
|
||||||
document.body.appendChild(panel);
|
document.body.appendChild(panel);
|
||||||
|
|
||||||
// Créer une infobulle pour les aperçus
|
const tooltip=document.createElement('div');
|
||||||
const tooltip = document.createElement('div');
|
tooltip.id='url-tooltip';
|
||||||
tooltip.id = 'url-tooltip';
|
Object.assign(tooltip.style,{
|
||||||
tooltip.style.position = 'absolute';
|
position:'absolute',background:'#fff',border:'1px solid #ccc',borderRadius:'5px',
|
||||||
tooltip.style.backgroundColor = '#fff';
|
padding:'10px',boxShadow:'0 0 10px rgba(0,0,0,.2)',maxWidth:'300px',
|
||||||
tooltip.style.border = '1px solid #ccc';
|
display:'none',zIndex:1001
|
||||||
tooltip.style.borderRadius = '5px';
|
});
|
||||||
tooltip.style.padding = '10px';
|
|
||||||
tooltip.style.boxShadow = '0px 0px 10px rgba(0,0,0,0.2)';
|
|
||||||
tooltip.style.maxWidth = '300px';
|
|
||||||
tooltip.style.display = 'none';
|
|
||||||
tooltip.style.zIndex = 1001;
|
|
||||||
document.body.appendChild(tooltip);
|
document.body.appendChild(tooltip);
|
||||||
|
|
||||||
// Structure de données pour stocker les URLs avec leurs métadonnées
|
const urlData={};
|
||||||
const urlData = {};
|
const comments=document.querySelectorAll('.commtext');
|
||||||
|
comments.forEach(comment=>{
|
||||||
// Extraire les URLs des commentaires et les mettre en évidence
|
const links=comment.querySelectorAll('a[href]');
|
||||||
const comments = document.querySelectorAll('.commtext');
|
links.forEach(link=>{
|
||||||
comments.forEach(comment => {
|
const u=link.href;
|
||||||
const links = comment.querySelectorAll('a[href]');
|
if(!urlData[u]) urlData[u]={url:u,count:0,upvotes:0,commentCount:0};
|
||||||
links.forEach(link => {
|
urlData[u].count++;
|
||||||
const url = link.href;
|
link.style.backgroundColor='#ffff99';
|
||||||
if (!urlData[url]) {
|
link.addEventListener('mouseover',()=>{
|
||||||
urlData[url] = {
|
|
||||||
url: url,
|
|
||||||
count: 1,
|
|
||||||
upvotes: 0 // Placeholder, à implémenter si les upvotes sont disponibles
|
|
||||||
};
|
|
||||||
link.style.backgroundColor = '#ffff99'; // Couleur de surbrillance initiale
|
|
||||||
|
|
||||||
// Ajouter l'URL à la liste dans le panneau
|
|
||||||
const listItem = document.createElement('li');
|
|
||||||
listItem.innerHTML = `<span>${url}</span> <span style="float: right; font-weight: bold;">1</span>`;
|
|
||||||
listItem.style.marginBottom = '5px';
|
|
||||||
listItem.style.wordWrap = 'break-word';
|
|
||||||
listItem.style.cursor = 'pointer';
|
|
||||||
listItem.dataset.url = url;
|
|
||||||
panel.querySelector('#url-list').appendChild(listItem);
|
|
||||||
} else {
|
|
||||||
urlData[url].count += 1;
|
|
||||||
// Mettre à jour le compteur dans le panneau
|
|
||||||
const existingItem = panel.querySelector(`li[data-url="${CSS.escape(url)}"] span:last-child`);
|
|
||||||
if (existingItem) {
|
|
||||||
existingItem.textContent = urlData[url].count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ajouter la fonctionnalité d'aperçu au survol des liens
|
|
||||||
link.addEventListener('mouseover', () => {
|
|
||||||
GM_xmlhttpRequest({
|
GM_xmlhttpRequest({
|
||||||
method: 'GET',
|
method:'GET',url:u,
|
||||||
url: url,
|
onload:r=>{
|
||||||
onload: response => {
|
tooltip.innerHTML='';
|
||||||
tooltip.innerHTML = '';
|
const d=new DOMParser().parseFromString(r.responseText,'text/html');
|
||||||
const parser = new DOMParser();
|
const t=d.querySelector('title')?d.querySelector('title').innerText:'No Title';
|
||||||
const doc = parser.parseFromString(response.responseText, 'text/html');
|
const desc=d.querySelector('meta[name="description"]')?d.querySelector('meta[name="description"]').content:'No Description';
|
||||||
const title = doc.querySelector('title') ? doc.querySelector('title').innerText : 'No Title';
|
tooltip.innerHTML=`<strong>${t}</strong><p>${desc}</p>`;
|
||||||
const description = doc.querySelector('meta[name="description"]') ? doc.querySelector('meta[name="description"]').content : 'No Description';
|
tooltip.style.display='block';
|
||||||
|
|
||||||
tooltip.innerHTML = `<strong>${title}</strong><p>${description}</p>`;
|
|
||||||
tooltip.style.display = 'block';
|
|
||||||
},
|
},
|
||||||
onerror: () => {
|
onerror:()=>{
|
||||||
tooltip.innerHTML = '<strong>Preview unavailable</strong>';
|
tooltip.innerHTML='<strong>Preview unavailable</strong>';
|
||||||
tooltip.style.display = 'block';
|
tooltip.style.display='block';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
const rect=link.getBoundingClientRect();
|
||||||
const rect = link.getBoundingClientRect();
|
tooltip.style.top=`${rect.bottom+window.scrollY+5}px`;
|
||||||
tooltip.style.top = `${rect.bottom + window.scrollY + 5}px`;
|
tooltip.style.left=`${rect.left+window.scrollX}px`;
|
||||||
tooltip.style.left = `${rect.left + window.scrollX}px`;
|
|
||||||
});
|
|
||||||
|
|
||||||
link.addEventListener('mouseout', () => {
|
|
||||||
tooltip.style.display = 'none';
|
|
||||||
});
|
});
|
||||||
|
link.addEventListener('mouseout',()=>{tooltip.style.display='none';});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Fonction pour mettre à jour l'affichage du panneau selon les filtres
|
function updatePanel(){
|
||||||
function updatePanel() {
|
const sortBy=panel.querySelector('#sort-select').value;
|
||||||
const sortBy = panel.querySelector('#sort-select').value;
|
const limit=panel.querySelector('#limit-select').value;
|
||||||
const limit = panel.querySelector('#limit-select').value;
|
let arr=Object.values(urlData);
|
||||||
|
if(sortBy==='frequency') arr.sort((a,b)=>b.count-a.count);
|
||||||
// Convertir l'objet en tableau pour le tri
|
else if(sortBy==='name') arr.sort((a,b)=>a.url.localeCompare(b.url));
|
||||||
let urlsArray = Object.values(urlData);
|
else if(sortBy==='upvotes') arr.sort((a,b)=>b.upvotes-a.upvotes);
|
||||||
|
else if(sortBy==='commentaires') arr.sort((a,b)=>b.commentCount-a.commentCount);
|
||||||
// Trier en fonction du critère sélectionné
|
if(limit!=='all') arr=arr.slice(0,parseInt(limit));
|
||||||
if (sortBy === 'frequency') {
|
const urlList=panel.querySelector('#url-list');
|
||||||
urlsArray.sort((a, b) => b.count - a.count);
|
urlList.innerHTML='';
|
||||||
} else if (sortBy === 'name') {
|
const counts=arr.map(i=>i.count);
|
||||||
urlsArray.sort((a, b) => a.url.localeCompare(b.url));
|
const maxC=Math.max(...counts),minC=Math.min(...counts);
|
||||||
} else if (sortBy === 'upvotes') {
|
arr.forEach(item=>{
|
||||||
urlsArray.sort((a, b) => b.upvotes - a.upvotes);
|
const li=document.createElement('li');
|
||||||
}
|
li.style.marginBottom='5px';li.style.wordWrap='break-word';li.style.cursor='pointer';
|
||||||
|
li.dataset.url=item.url;
|
||||||
// Appliquer la limite si nécessaire
|
const r=(item.count-minC)/(maxC-minC+1);
|
||||||
if (limit !== 'all') {
|
li.style.backgroundColor=r>0.66?COLORS.high:r>0.33?COLORS.medium:COLORS.low;
|
||||||
const limitNumber = parseInt(limit);
|
li.innerHTML=`
|
||||||
urlsArray = urlsArray.slice(0, limitNumber);
|
|
||||||
}
|
|
||||||
|
|
||||||
const urlList = panel.querySelector('#url-list');
|
|
||||||
urlList.innerHTML = ''; // Vider la liste actuelle
|
|
||||||
|
|
||||||
// Déterminer les fréquences pour la coloration
|
|
||||||
const counts = urlsArray.map(item => item.count);
|
|
||||||
const maxCount = Math.max(...counts);
|
|
||||||
const minCount = Math.min(...counts);
|
|
||||||
|
|
||||||
urlsArray.forEach(item => {
|
|
||||||
const listItem = document.createElement('li');
|
|
||||||
listItem.style.marginBottom = '5px';
|
|
||||||
listItem.style.wordWrap = 'break-word';
|
|
||||||
listItem.style.cursor = 'pointer';
|
|
||||||
listItem.dataset.url = item.url;
|
|
||||||
|
|
||||||
// Définir la couleur en fonction de la fréquence
|
|
||||||
let color;
|
|
||||||
const ratio = (item.count - minCount) / (maxCount - minCount + 1);
|
|
||||||
if (ratio > 0.66) {
|
|
||||||
color = COLORS.high;
|
|
||||||
} else if (ratio > 0.33) {
|
|
||||||
color = COLORS.medium;
|
|
||||||
} else {
|
|
||||||
color = COLORS.low;
|
|
||||||
}
|
|
||||||
listItem.style.backgroundColor = color;
|
|
||||||
|
|
||||||
// Créer le contenu avec URL et score
|
|
||||||
listItem.innerHTML = `
|
|
||||||
<span>${item.url}</span>
|
<span>${item.url}</span>
|
||||||
<span style="float: right; font-weight: bold;">${item.count}${item.upvotes > 0 ? ` / ${item.upvotes}` : ''}</span>
|
<span style="float:right;font-weight:bold;">
|
||||||
|
${item.count}${item.upvotes>0?` / ${item.upvotes}`:''}${item.commentCount>0?` / ${item.commentCount}`:''}
|
||||||
|
</span>
|
||||||
`;
|
`;
|
||||||
|
li.addEventListener('click',()=>{
|
||||||
// Ajouter l'événement de clic pour copier l'URL
|
GM_setClipboard(item.url,'text');
|
||||||
listItem.addEventListener('click', () => {
|
alert('URL copiée.');
|
||||||
GM_setClipboard(item.url, 'text');
|
|
||||||
alert('URL copiée dans le presse-papiers!');
|
|
||||||
});
|
});
|
||||||
|
urlList.appendChild(li);
|
||||||
urlList.appendChild(listItem);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialiser le panneau
|
|
||||||
updatePanel();
|
updatePanel();
|
||||||
|
panel.querySelector('#sort-select').addEventListener('change',updatePanel);
|
||||||
// Écouter les changements dans les sélecteurs de tri et de limite
|
panel.querySelector('#limit-select').addEventListener('change',updatePanel);
|
||||||
panel.querySelector('#sort-select').addEventListener('change', updatePanel);
|
toggleButton.addEventListener('click',()=>{
|
||||||
panel.querySelector('#limit-select').addEventListener('change', updatePanel);
|
if(panel.style.display==='none'){panel.style.display='block';toggleButton.textContent='Masquer les URLs';}
|
||||||
|
else{panel.style.display='none';toggleButton.textContent='Afficher les URLs';}
|
||||||
// Gérer le bouton de bascule pour afficher/masquer le panneau
|
});
|
||||||
toggleButton.addEventListener('click', () => {
|
panel.querySelector('#copy-urls').addEventListener('click',()=>{
|
||||||
if (panel.style.display === 'none') {
|
GM_setClipboard(Object.keys(urlData).join('\n'),'text');
|
||||||
panel.style.display = 'block';
|
alert('URLs copiées.');
|
||||||
toggleButton.textContent = 'Masquer les URLs';
|
|
||||||
} else {
|
|
||||||
panel.style.display = 'none';
|
|
||||||
toggleButton.textContent = 'Afficher les URLs';
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Gérer le bouton "Copier les URLs"
|
const commentRows=document.querySelectorAll('tr.comtr');
|
||||||
panel.querySelector('#copy-urls').addEventListener('click', () => {
|
commentRows.forEach(row=>{
|
||||||
const urlArray = Object.keys(urlData);
|
const commentLink=row.querySelector('a[href^="item?id="]');
|
||||||
GM_setClipboard(urlArray.join('\n'), 'text');
|
if(commentLink){
|
||||||
alert('URLs copiées dans le presse-papiers!');
|
const pointsSpan=row.querySelector('.score');
|
||||||
});
|
const pts=pointsSpan?parseInt(pointsSpan.textContent):0;
|
||||||
|
const c=row.querySelector('.commtext');
|
||||||
// Optionnel : Collecter les upvotes des commentaires pour chaque URL
|
if(c){
|
||||||
// Note : Hacker News n'expose pas directement les upvotes des commentaires via le DOM.
|
const links=c.querySelectorAll('a[href]');
|
||||||
// Si vous avez une méthode pour obtenir les upvotes, vous pouvez l'implémenter ici.
|
links.forEach(l=>{if(urlData[l.href]) urlData[l.href].upvotes+=pts;});
|
||||||
// Par exemple, si les points des commentaires sont visibles, vous pouvez les extraire et les attribuer aux URLs.
|
|
||||||
|
|
||||||
// Exemple de récupération des points des commentaires (si disponibles)
|
|
||||||
const commentRows = document.querySelectorAll('tr.comtr');
|
|
||||||
commentRows.forEach(row => {
|
|
||||||
const commentLink = row.querySelector('a[href^="item?id="]');
|
|
||||||
if (commentLink) {
|
|
||||||
const pointsSpan = row.querySelector('.score');
|
|
||||||
const points = pointsSpan ? parseInt(pointsSpan.textContent) : 0;
|
|
||||||
const comment = row.querySelector('.commtext');
|
|
||||||
if (comment) {
|
|
||||||
const links = comment.querySelectorAll('a[href]');
|
|
||||||
links.forEach(link => {
|
|
||||||
const url = link.href;
|
|
||||||
if (urlData[url]) {
|
|
||||||
urlData[url].upvotes += points;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Mettre à jour le panneau après avoir collecté les upvotes
|
const childrenMap={};
|
||||||
|
commentRows.forEach(r=>{
|
||||||
|
const id=r.id;const pid=r.getAttribute('data-parent');
|
||||||
|
if(!pid||!id) return;
|
||||||
|
if(!childrenMap[pid]) childrenMap[pid]=[];
|
||||||
|
childrenMap[pid].push(id);
|
||||||
|
});
|
||||||
|
const cache={};
|
||||||
|
function size(id){
|
||||||
|
if(!childrenMap[id]) return 0;
|
||||||
|
if(cache[id]!=null) return cache[id];
|
||||||
|
let s=childrenMap[id].length;
|
||||||
|
childrenMap[id].forEach(k=>{s+=size(k);});
|
||||||
|
cache[id]=s;return s;
|
||||||
|
}
|
||||||
|
commentRows.forEach(r=>{
|
||||||
|
const id=r.id;
|
||||||
|
const c=r.querySelector('.commtext');
|
||||||
|
if(!id||!c) return;
|
||||||
|
const count=size(id);
|
||||||
|
const links=c.querySelectorAll('a[href]');
|
||||||
|
links.forEach(l=>{if(urlData[l.href]) urlData[l.href].commentCount+=count;});
|
||||||
|
});
|
||||||
|
|
||||||
updatePanel();
|
updatePanel();
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user