initial nostreratu

This commit is contained in:
Ludger Sicking 2024-08-20 11:35:23 +02:00
commit da2cfb1bb8
7 changed files with 119 additions and 0 deletions

7
background.js Normal file
View file

@ -0,0 +1,7 @@
// background.js
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === "ldJsonData") {
chrome.storage.local.set({ ldJsonData: message.data });
}
});

55
contentScript.js Normal file
View file

@ -0,0 +1,55 @@
// contentScript.js
// Function to find and return the content of ld+json scripts
function findLDJSONScripts() {
const scripts = document.querySelectorAll('script[type="application/ld+json"]');
//const scripts = document.querySelectorAll('class');
const jsonData = [];
//console.log(scripts); // Steffen debugging
scripts.forEach(script => {
try {
const json = JSON.parse(script.textContent);
jsonData.push(json);
} catch (e) {
console.error("Error parsing JSON-LD", e);
}
});
return jsonData;
}
window.addEventListener('load', myMain);
function myMain() {
//alert('Die Seite wurde vollständig geladen!');
console.log("DOM fully loaded and parsed");
const ldJsonData = findLDJSONScripts();
if (ldJsonData.length > 0) {
console.log("LD+JSON scripts found:", ldJsonData);
// Option 1: Render on the same page (e.g., append to body)
const pre = document.createElement('pre');
pre.textContent = JSON.stringify(ldJsonData, null, 2);
document.body.appendChild(pre);
// Option 2: Send data to the background script for the popup
chrome.runtime.sendMessage({type: "ldJsonData", data: ldJsonData});
}
}
// //window.onload
// window.addEventListener('load', Meldung);
// //document.addEventListener("DOMContentLoaded", function() {
// //window.addEventListener("load", function() {
// //document.addEventListener("DOMContentLoaded", function() {
// // Your code here will run after the DOM is fully loaded
// });
// Send the extracted JSON-LD data to the popup or render it directly
//const ldJsonData = findLDJSONScripts();

BIN
icons/icon128.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
icons/icon48.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

26
manifest.json Normal file
View file

@ -0,0 +1,26 @@
{
"manifest_version": 3,
"name": "LD+JSON Script Checker",
"version": "1.0",
"description": "Checks for ld+json script tags and renders their content.",
"permissions": [
"activeTab"
],
"action": {
"default_popup": "popup.html",
"default_icon": {
"48": "icons/icon48.png",
"128": "icons/icon128.png"
}
},
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["contentScript.js"]
}
]
}

19
popup.html Normal file
View file

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LD+JSON Content</title>
<style>
pre {
white-space: pre-wrap;
word-wrap: break-word;
}
</style>
</head>
<body>
<h2>LD+JSON Content</h2>
<div id="content"></div>
<script src="popup.js"></script>
</body>
</html>

12
popup.js Normal file
View file

@ -0,0 +1,12 @@
// popup.js
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === "ldJsonData") {
const contentDiv = document.getElementById('content');
request.data.forEach(json => {
const pre = document.createElement('pre');
pre.textContent = JSON.stringify(json, null, 2);
contentDiv.appendChild(pre);
});
}
});