Last active
June 5, 2024 19:19
-
-
Save lwcorp/fd39a987b51058c3a2937bfd553ab1f0 to your computer and use it in GitHub Desktop.
Adds a download button to the Domain/App Category Classifier of chrome://topics-internals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:adder();function adder(){var a="[slot=\"tab\"]:nth-of-type(2)";a=document.querySelectorAll(a),0<a.length&&(a=a[0],"false"==a.getAttribute("aria-selected")&&a.click(),addDownloadButton())}function addDownloadButton(){var a;if(a=document.querySelectorAll("#download"),0==a.length){let a=document.createElement("button");a.id="#download".replace(/[#\.]/,""),a.textContent="Download",a.href="#",a.onclick=function(){return downloader(),!1};let b=document.querySelector("#hosts-classification-button");b.parentNode.insertBefore(a,b.nextSibling)}}function downloader(){let a="#hosts-classification-result-table",b="td";if(b=a+" "+b,a=document.querySelectorAll(a),0<a.length&&0<document.querySelectorAll(b).length){let b=formatTableToCSV(a[0]),c=new Blob([b],{type:"text/csv;charset=utf-8;"}),d=document.createElement("a");d.href=URL.createObjectURL(c),d.setAttribute("download","classification_results.csv"),document.body.appendChild(d),d.click(),document.body.removeChild(d)}}function formatTableToCSV(a){let b=[],c=a.querySelectorAll("tr"),d=c[0].querySelectorAll("th"),e=Array.from(d).map(a=>`"${a.textContent}"`).concat("\"Topic Names\"").join(",");b.push(e);for(let d=1;d<c.length;d++){let a=c[d].querySelectorAll("td"),e=`"${a[0].textContent}"`,f=a[1].querySelectorAll("span");0===f.length?b.push(`${e},"",""`):f.forEach(a=>{let[c,...d]=a.textContent.split(". "),f=d.join(". ");b.push(`${e},"${c}","${f}"`)})}return b.join("\n")} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
adder(); | |
function adder() { | |
var tab='[slot="tab"]:nth-of-type(2)', focus='aria-selected'; | |
tab=document.querySelectorAll(tab); | |
if (tab.length>0) { | |
tab=tab[0]; | |
if (tab.getAttribute(focus)=='false') | |
tab.click() | |
addDownloadButton(); | |
} | |
} | |
function addDownloadButton() { | |
var orig='#hosts-classification-button', elm='#download', elmTag; | |
elmTag=document.querySelectorAll(elm) | |
if (elmTag.length==0) { | |
let button = document.createElement('button'); | |
button.id = elm.replace(/[#\.]/, ''); | |
button.textContent = 'Download'; | |
button.href = '#'; | |
button.onclick = function() { | |
downloader(); | |
return false; | |
}; | |
let referenceNode = document.querySelector(orig); | |
referenceNode.parentNode.insertBefore(button, referenceNode.nextSibling); | |
} | |
} | |
function downloader() { | |
let table = '#hosts-classification-result-table', tableRows='td', filename='classification_results.csv'; | |
tableRows = table + ' ' + tableRows; | |
table=document.querySelectorAll(table); | |
if (table.length>0 && document.querySelectorAll(tableRows).length>0) { | |
let csvOutput = formatTableToCSV(table[0]); | |
let blob = new Blob([csvOutput], { type: 'text/csv;charset=utf-8;' }); | |
let link = document.createElement('a'); | |
link.href = URL.createObjectURL(blob); | |
link.setAttribute('download', filename); | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
} | |
} | |
function formatTableToCSV(table) { | |
let csvRows = []; | |
// Get all rows from the table | |
let rows = table.querySelectorAll('tr'); | |
// Process header row | |
let headerCells = rows[0].querySelectorAll('th'); | |
let headerRow = Array.from(headerCells).map(cell => `"${cell.textContent}"`).concat('"Topic Names"').join(','); | |
csvRows.push(headerRow); | |
// Process data rows | |
for (let i = 1; i < rows.length; i++) { | |
let cells = rows[i].querySelectorAll('td'); | |
let host = `"${cells[0].textContent}"`; | |
let topics = cells[1].querySelectorAll('span'); | |
if (topics.length === 0) { | |
// If there are no spans, push an empty code and description | |
csvRows.push(`${host},"",""`); | |
} else { | |
// Create separate CSV row for each topic entry | |
topics.forEach(topic => { | |
let [code, ...description] = topic.textContent.split('. '); | |
let descriptionText = description.join('. '); // Rejoin the description if it was split | |
csvRows.push(`${host},"${code}","${descriptionText}"`); | |
}); | |
} | |
} | |
// Combine all rows into a single CSV string | |
return csvRows.join('\n'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment