+violentmonkey user scripts

This commit is contained in:
anon
2024-09-02 12:39:38 +02:00
parent d2e6f42a90
commit 8bcc124dd9
2 changed files with 152 additions and 0 deletions

79
userscripts/container.js Normal file
View File

@ -0,0 +1,79 @@
// ==UserScript==
// @name Container
// @namespace Violentmonkey Scripts
// @match *://*/*
// @grant none
// @version 1.0
// @author Anon
// @description Creates a floating container where other scripts can register themselfs. The idea is that some functionalities should only be ran if the user explicitly asks for it.
// ==/UserScript==
(function() {
'use strict';
const containerHTML = `
<div id="violent-monkey-manual-script-container" style="
width: 200px;
position: fixed;
top: 20px;
right: 20px;
background-color: #f0f0f0;
border: 1px solid #ccc;
padding: 10px;
z-index: 1000;
display: none;
">
<div style="
font-weight: bold;
margin-bottom: 5px;
">
Violentmonkey Manual Script Container
</div>
<hr/>
<div id="violent-monkeys-manual-script-container-main">
</div>
</div>
`;
document.body.insertAdjacentHTML('beforeend', containerHTML);
// Add event listener for Ctrl + P to toggle the container
document.addEventListener('keydown', function(e) {
if (e.ctrlKey && e.code === 'KeyP') {
e.preventDefault();
(function toggleContainer() {
const container = document.getElementById('violent-monkey-manual-script-container');
container.style.display = container.style.display === 'none' ? 'block' : 'none';
})();
}
});
window.addContainerFunctionality = function(buttonText, callback) {
const containerMain = document.getElementById('violent-monkeys-manual-script-container-main');
const functionalityHTML = `
<div style="margin-bottom: 5px;">
<button onclick="(${callback})();" style="
color: white;
background: red;
width: 100%;
">${buttonText}</button>
</div>
`;
containerMain.insertAdjacentHTML('beforeend', functionalityHTML);
};
document.dispatchEvent(new CustomEvent('violent-monkey-container-ready', {}));
console.log("--- container initialized");
})();
/* Standard way to register a funtionality from another script:
*/
// // Container loaded firsts
// if (typeof window.addContainerFunctionality === 'function') {
// addContainerFunctionality('<functionality-name>', 'window.<my>_main');
// } else { // We must wait for the container to load
// document.addEventListener('violent-monkey-container-ready', function(e) {
// addContainerFunctionality('<functionality-name>', 'window.<my>_main');
// });
// }

73
userscripts/inliner.js Normal file
View File

@ -0,0 +1,73 @@
// ==UserScript==
// @name Inliner
// @namespace Violentmonkey Scripts
// @match *://*/*
// @grant none
// @version 1.0
// @author Anon
// @description The primary use of this script is to enhance the SingleFile extension. Currently only Rentry links work (as thats all i needed).
// ==/UserScript==
(function() {
window.inliner_main = () => {
// Convert text links to <a> links
var iterator = document.createNodeIterator(
document.documentElement,
NodeFilter.SHOW_TEXT,
{ acceptNode: function(node) { return NodeFilter.FILTER_ACCEPT; }}
);
var nodeList = [];
var currentNode;
const regex = /https:\/\/rentry.org\/\w*/
while ((currentNode = iterator.nextNode())) {
const match = regex.exec(currentNode.nodeValue);
if (match == null) { continue; }
nodeList.push(currentNode);
}
console.log(`--- indexer # found ${nodeList.length} links`);
for (var i of nodeList) {
const match = regex.exec(i.nodeValue);
var tempDiv = document.createElement('div');
tempDiv.innerHTML =
i.nodeValue.replace(
match[0],
`<a href="${match[0]}">` + match[0] + '</a>'
)
;
i.replaceWith(tempDiv);
}
// Actual inlining
const links = document.querySelectorAll('a');
links.forEach(link => {
const href = link.getAttribute('href');
if (href && href.startsWith('https://rentry.org/')) {
const iframeHTML = `
<iframe
src="${href}"
style="width: 100%; height: 500px; border: none;"
loading="lazy">
</iframe>
`;
link.parentNode.insertAdjacentHTML('beforeend', iframeHTML);
}
console.log("--- inliner # inlined link");
});
}
if (typeof window.addContainerFunctionality === 'function') {
addContainerFunctionality('Inline links', 'window.inliner_main');
} else {
document.addEventListener('violent-monkey-container-ready', function(e) {
addContainerFunctionality('Inline links', 'window.inliner_main');
});
}
})();