Find and correct any errors in the following code:
- User
Find and correct any errors in the following code:
- Assistant
Of course! Please provide the code you would like me to review and help correct.
- User
- Assistant
Find and correct any errors in the following code:
Of course! Please provide the code you would like me to review and help correct.
// ==UserScript== // @name YT2Invidio // @namespace de.izzysoft // @author Izzy // @description Point YouTube links to Invidious, Twitter to Nitter, Instagram to Bibliogram, Reddit to Teddit, Medium to Scribe // @license CC BY-NC-SA // @include * // @version 1.6.3 // @run-at document-idle // @grant GM.getValue // @grant GM.setValue // @grant GM_getValue // @grant GM_setValue // @grant GM_registerMenuCommand // @grant GM.registerMenuCommand // @grant GM_openInTab // @grant GM.openInTab // @grant unsafeWindow // @homepageURL https://codeberg.org/izzy/userscripts // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js // @downloadURL https://update.greasyfork.org/scripts/374833/YT2Invidio.user.js // @updateURL https://update.greasyfork.org/scripts/374833/YT2Invidio.meta.js // ==/UserScript==
// Default Config const defaultConfig = { hosts: {invidious: "inv.nadeko.net", nitter: "xcancel.com", bibliogram: "piokok.com", teddit: "reddit.com", scribe: "scribe.rip"}, invProxy: 0 }; /* console.log(defaultConfig.hosts); console.log(obj.hosts.hasOwnProperty('bibliogram')); // true */
// Get the Invidious instance to use for rewrite function doRewrite() { GM.getValue('YT2IConfig',JSON.stringify(defaultConfig)).then(function(result) { rewriteLinks(result); }); } doRewrite();
// Do the actual rewrite
function rewriteLinks(config) {
console.log(Using '${config}' for rewrite);
var cfg = JSON.parse(config);
var videohost = cfg.hosts.invidious;
var nitterhost = cfg.hosts.nitter;
var bibliogramhost = cfg.hosts.bibliogram;
var teddithost = cfg.hosts.teddit;
var scribehost = cfg.hosts.scribe || 'scribe.rip';
var invProxy = 'local=0';
if ( cfg.invProxy == 1 ) { invProxy = 'local=1'; }
console.log('Invidious: '+videohost+', Params: '+invProxy);
console.log('Nitter: '+nitterhost);
console.log('Bibliogram: '+bibliogramhost);
console.log('Teddit: '+teddithost);
console.log('Scribe: '+scribehost)
// --=[ document links ]=--
console.log('Checking '+document.links.length+' links for YT, Twitter, Insta, Reddit & Co.');
for(var i = 0; i < document.links.length; i++) {
var elem = document.links[i];
// Youtube: https://www.youtube.com/watch?v=cRRA2xRRgl8 || https://www.youtube.com/channel/dfqwfhqQ34er || https://www.youtube.com/playlist?list=PLjV3HijScGMynGvjJrvNNd5Q9pPy255dL
// only rewrite if we're not on Invidious already (too keep the "watch this on YouTube" links intact)
if (videohost != '' && elem.href.match(/((www|m)\.)?youtube.com(\/(watch\?v|playlist\?list)=[a-z0-9_-]+)/i)) {
if (location.hostname != videohost) { elem.href='https://'+videohost+RegExp.$3+'&'+invProxy; }
} else if (videohost != '' && elem.href.match(/((www|m)\.)?youtu.be\/([a-z0-9_-]+)/i)) {
if (location.hostname != videohost) { elem.href='https://'+videohost+'/watch?v='+RegExp.$3+'?'+invProxy; }
} else if (videohost != '' && elem.href.match(/((www|m)\.)?youtube.com(\/channel\/[a-z0-9_-]+)/i)) {
if (location.hostname != videohost) { elem.href='https://'+videohost+RegExp.$3+'?'+invProxy; }
// Twitter
} else if (nitterhost != '' && elem.href.match(/(mobile\.)?x\.com\/([^&#]+)/i)) {
if (location.hostname != nitterhost) elem.href='https://'+nitterhost+'/'+RegExp.$2;
}
// Bibliogram
else if (bibliogramhost != '' && elem.href.match(/(www\.)?instagram\.com\/(p|tv)\/([^&#/]+)/i)) { // profile
if (location.hostname != bibliogramhost) {
elem.href = 'https://'+bibliogramhost+'/p/' + RegExp.$3;
}
} else if (bibliogramhost != '' && elem.href.match(/(www\.)?instagram\.com\/([^&#/]+)/i)) { // image or video
if (location.hostname != bibliogramhost) {
elem.href = 'https://'+bibliogramhost+'/u/' + RegExp.$2;
}
}
// Teddit
else if (teddithost != '' && elem.href.match(/((www|old)\.)?reddit.com\/(.*)/i)) {
if (location.hostname != teddithost) { elem.href = 'https://'+teddithost+'/'+RegExp.$3; }
}
else if (teddithost != '' && elem.href.match(/(r.nf)\/(.*)/i)) {
if (location.hostname != teddithost) { elem.href = 'https://'+teddithost+'/'+RegExp.$2; }
}
// Scribe
else if (scribehost != '' && elem.href.match(/(www\.)?medium\.com\/([^&#]+)/i)) {
if (location.hostname != scribehost) elem.href='https://'+scribehost+'/'+RegExp.$2;
}
}
// --=[ embedded links ]=-- // based on https://greasyfork.org/en/scripts/394841-youtube-to-invidio-us-embed if (videohost != '') { var src, dataSrc, iframes = document.getElementsByTagName('iframe'); var embProxy console.log('Checking '+iframes.length+' frames for embedded videos'); for (var i = 0; i < iframes.length; i++) { src = iframes[i].getAttribute('src'); dataSrc = false; if ( src == null ) { src = iframes[i].getAttribute('data-s9e-mediaembed-src'); dataSrc = true; } if ( src == null ) continue; if ( src.match(/((www|m).)?youtube.com(/(watch?v|playlist?list)=[a-z0-9_-]+)/i) || src.match(/((www|m).)?youtube.com(/(channel|embed)/[a-z0-9_-]+)/i) ) { if ( RegExp.4 == 'embed' ) { embProxy = '?'+invProxy; } else { embProxy = '&'+invProxy; } if ( dataSrc ) { iframes[i].setAttribute('data-s9e-mediaembed-src','https://'+videohost+RegExp.3+embProxy); } // iframes[i].setAttribute('style', 'min-height:100%; min-width:100%;'); iframes[i].setAttribute('frameborder', '0'); iframes[i].setAttribute('allowfullscreen', '1'); } } }
console.log('Rewrite done.'); }
// Give the user the possibility to set a different preferred instance // A list of available instances can be found at // https://github.com/omarroth/invidious/wiki/Invidious-Instances // https://github.com/zedeus/nitter/wiki/Instances // https://github.com/cloudrac3r/bibliogram/wiki/Instances async function setInvidiousInstance() { let cfgs = await GM.getValue('YT2IConfig',JSON.stringify(defaultConfig)); cfg = JSON.parse(cfgs); var vhost = prompt('Set Invidious instance to:', cfg.hosts.invidious); if ( vhost == '' || vhost.match(/^(https?)?:?[/](.+?)(/.)?/) ) { if ( vhost == '' ) { cfg.hosts.invidious = ''; } else { cfg.hosts.invidious = RegExp.2; } GM.setValue('YT2IConfig',JSON.stringify(cfg)); } } async function setNitterInstance() { let cfgs = await GM.getValue('YT2IConfig',JSON.stringify(defaultConfig)); cfg = JSON.parse(cfgs); var vhost = prompt('Set Nitter instance to:', cfg.hosts.nitter); if ( vhost == '' || vhost.match(/^(https?)?:?[/](.+?)(/.)?/) ) { if ( vhost == '' ) { cfg.hosts.nitter = ''; } else { cfg.hosts.nitter = RegExp.2; } GM.setValue('YT2IConfig',JSON.stringify(cfg)); } } async function setBibliogramInstance() { let cfgs = await GM.getValue('YT2IConfig',JSON.stringify(defaultConfig)); cfg = JSON.parse(cfgs); var vhost = prompt('Set Bibliogram instance to:', cfg.hosts.bibliogram); if ( vhost == '' || vhost.match(/^(https?)?:?[/](.+?)(/.)?/) ) { if ( vhost == '' ) { cfg.hosts.bibliogram = ''; } else { cfg.hosts.bibliogram = RegExp.2; } GM.setValue('YT2IConfig',JSON.stringify(cfg)); } } async function toggleInvidiousProxy() { let cfgs = await GM.getValue('YT2IConfig',JSON.stringify(defaultConfig)); cfg = JSON.parse(cfgs); if ( cfg.invProxy == 1 ) { cfg.invProxy = 0; console.log('Invidious proxying turned off.'); } else { cfg.invProxy = 1; console.log('Invidious proxying turned on.'); } GM.setValue('YT2IConfig',JSON.stringify(cfg)); } async function setTedditInstance() { let cfgs = await GM.getValue('YT2IConfig',JSON.stringify(defaultConfig)); cfg = JSON.parse(cfgs); var vhost = prompt('Set Teddit instance to:', cfg.hosts.teddit); if ( vhost == '' || vhost.match(/^(https?)?:?[/](.+?)(/.)?/) ) { if (vhost=='') { cfg.hosts.teddit = ''; } else { cfg.hosts.teddit = RegExp.2; } GM.setValue('YT2IConfig',JSON.stringify(cfg)); } } async function setScribeInstance() { let cfgs = await GM.getValue('YT2IConfig',JSON.stringify(defaultConfig)); cfg = JSON.parse(cfgs); var vhost = prompt('Set Scribe instance to:', cfg.hosts.scribe); if ( vhost == '' || vhost.match(/^(https?)?:?[/])?2; } GM.setValue('YT2IConfig',JSON.stringify(cfg)); } }
// open tab with instance list from Invidious/Nitter/Bibliogram/Teddit wiki function openInvidiousList() { GM.openInTab('https://github.com/iv-org/documentation/blob/master/Invidious-Instances.md', { active: true, insert: true }); } function openNitterList() { GM.openInTab('https://github.com/zedeus/nitter/wiki/Instances', { active: true, insert: true }); } function openBibliogramList() { GM.openInTab('https://git.sr.ht/~cadence/bibliogram-docs/tree/master/docs/Instances.md', { active: true, insert: true }); } function openTedditList() { GM.openInTab('https://codeberg.org/teddit/teddit#instances', { active: true, insert: true }); }
if ( location.href != 'https://duckduckgo.com/post3.html' ) { // somehow this is called from within a search and causes our script to run again GM.registerMenuCommand('Set Invidious instance',setInvidiousInstance); GM.registerMenuCommand('Show list of known Invidious instances', openInvidiousList ); GM.registerMenuCommand('Toggle Invidious proxy state', toggleInvidiousProxy); GM.registerMenuCommand('Set Nitter instance',setNitterInstance); GM.registerMenuCommand('Show list of known Nitter instances', openNitterList ); GM.registerMenuCommand('Set Bibliogram instance',setBibliogramInstance); GM.registerMenuCommand('Show list of known Bibliogram instances', openBibliogramList ); GM.registerMenuCommand('Set Teddit instance',setTedditInstance); GM.registerMenuCommand('Show list of known Teddit instances', openTedditList ); GM.registerMenuCommand('Set Scribe instance',setScribeInstance);
GM.registerMenuCommand('Rewrite now', doRewrite); console.log('registered menus.'); }
// register keyboard shortcut Alt+Y to trigger rewrite document.onkeyup=function(e) { var e = e || window.event; // for IE to cover IEs window event-object if(e.altKey && e.which == 89) { doRewrite(); return false; } }
This script is a UserScript meant for use with userscript managers like Tampermonkey or Greasemonkey and is designed to rewrite specific links to use alternative front-ends. I'll provide some corrections and improvements:
Redundant Grants: If you're targeting Greasemonkey v4+ or Tampermonkey, you don't need both GM.* and GM_ equivalent. Remove the duplicates:
GM_getValue and GM_setValue if GM.getValue and GM.setValue are used.GM_registerMenuCommand if GM.registerMenuCommand is used.GM_openInTab if GM.openInTab is used.Ensure Consistent Host Updates for Social Media: There are a few duplicated blocks for matching and rewriting the host links that could leave typos around the host updates. Make sure patterns are consistently checked or consolidated.
Console Logs & Comments: You have some redundant console.log statements and commented-out code. You could clean them up for clarity.
Remove Unnecessary Commented Code: Lines like // iframes[i].setAttribute('style', 'min-height:100%; min-width:100%;'); should be removed if not needed.
Here's a revised version with these changes applied:
// ==UserScript==
// @name YT2Invidio
// @namespace de.izzysoft
// @description Point YouTube links to Invidious, Twitter to Nitter, Instagram to Bibliogram, Reddit to Teddit, Medium to Scribe
// @license CC BY-NC-SA
// @include *
// @version 1.6.3
// @run-at document-idle
// @grant GM.getValue
// @grant GM.setValue
// @grant GM.registerMenuCommand
// @grant GM.openInTab
// @grant unsafeWindow
// @homepageURL https://codeberg.org/izzy/userscripts
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
// @downloadURL https://update.greasyfork.org/scripts/374833/YT2Invidio.user.js
// @updateURL https://update.greasyfork.org/scripts/374833/YT2Invidio.meta.js
// ==/UserScript==
// Default Config
const defaultConfig = {
hosts: { invidious: "inv.nadeko.net", nitter: "xcancel.com", bibliogram: "piokok.com", teddit: "reddit.com", scribe: "scribe.rip" },
invProxy: 0
};
// Get the Invidious instance to use for rewrite
function doRewrite() {
GM.getValue('YT2IConfig', JSON.stringify(defaultConfig)).then(result => {
rewriteLinks(result);
});
}
doRewrite();
// Do the actual rewrite
function rewriteLinks(config) {
var cfg = JSON.parse(config);
var { invidious: videohost, nitter: nitterhost, bibliogram: bibliogramhost, teddit: teddithost, scribe: scribehost } = cfg.hosts;
var invProxy = cfg.invProxy ? 'local=1' : 'local=0';
console.log('Checking ' + document.links.length + ' links...');
for (let elem of document.links) {
if (videohost && elem.href.match(/((www|m)\.)?youtube.com(\/(watch\?v|playlist\?list)=[a-z0-9_-]+)/i) && location.hostname != videohost) {
elem.href = `https://${videohost}${RegExp.$3}&${invProxy}`;
} else if (videohost && elem.href.match(/((www|m)\.)?youtu.be\/([a-z0-9_-]+)/i) && location.hostname != videohost) {
elem.href = `https://${videohost}/watch?v=${RegExp.$3}&${invProxy}`;
} else if (videohost && elem.href.match(/((www|m)\.)?youtube.com(\/channel\/[a-z0-9_-]+)/i) && location.hostname != videohost) {
elem.href = `https://${videohost}${RegExp.$3}?${invProxy}`;
} else if (nitterhost && elem.href.match(/(mobile\.)?x\.com\/([^&#]+)/i) && location.hostname != nitterhost) {
elem.href = `https://${nitterhost}/${RegExp.$2}`;
} else if (bibliogramhost && elem.href.match(/(www\.)?instagram\.com\/(p|tv)\/([^&#/]+)/i) && location.hostname != bibliogramhost) {
elem.href = `https://${bibliogramhost}/p/${RegExp.$3}`;
} else if (bibliogramhost && elem.href.match(/(www\.)?instagram\.com\/([^&#/]+)/i) && location.hostname != bibliogramhost) {
elem.href = `https://${bibliogramhost}/u/${RegExp.$2}`;
} else if (teddithost && elem.href.match(/((www|old)\.)?reddit.com\/(.*)/i) && location.hostname != teddithost) {
elem.href = `https://${teddithost}/${RegExp.$3}`;
} else if (teddithost && elem.href.match(/(r.nf)\/(.*)/i) && location.hostname != teddithost) {
elem.href = `https://${teddithost}/${RegExp.$2}`;
} else if (scribehost && elem.href.match(/(www\.)?medium\.com\/([^&#]+)/i) && location.hostname != scribehost) {
elem.href = `https://${scribehost}/${RegExp.$2}`;
}
}
if (videohost) {
let iframes = document.getElementsByTagName('iframe');
console.log('Checking ' + iframes.length + ' frames for embedded videos');
for (let iframe of iframes) {
let src = iframe.getAttribute('src') || iframe.getAttribute('data-s9e-mediaembed-src');
let embProxy = src && src.match(/((www|m)\.)?youtube.com(\/(watch\?v|playlist\?list|channel|embed)=[a-z0-9_-]+)/i) ? `?${invProxy}` : `&${invProxy}`;
if (src) {
iframe.setAttribute(src ? 'data-s9e-mediaembed-src' : 'src', `https://${videohost}${RegExp.$3}${embProxy}`);
iframe.setAttribute('frameborder', '0');
iframe.setAttribute('allowfullscreen', '1');
}
}
}
console.log('Rewrite done.');
}
// Helper function for setting instances
async function setInstance(type) {
let cfg = JSON.parse(await GM.getValue('YT2IConfig', JSON.stringify(defaultConfig)));
let newHost = prompt(`Set ${type} instance to:`, cfg.hosts[type]);
if (newHost !== null && newHost.match(/^(https?)?:?[\/]*(.+?)(\/.*)?$/)) {
cfg.hosts[type] = newHost ? RegExp.$2 : '';
GM.setValue('YT2IConfig', JSON.stringify(cfg));
}
}
// Register menu commands
if (location.href !== 'https://duckduckgo.com/post3.html') {
GM.registerMenuCommand('Set Invidious instance', () => setInstance('invidious'));
GM.registerMenuCommand('Show list of known Invidious instances', () => GM.openInTab('https://github.com/iv-org/documentation/blob/master/Invidious-Instances.md', { active: true, insert: true }));
GM.registerMenuCommand('Toggle Invidious proxy state', toggleInvidiousProxy);
GM.registerMenuCommand('Set Nitter instance', () => setInstance('nitter'));
GM.registerMenuCommand('Show list of known Nitter instances', () => GM.openInTab('https://github.com/zedeus/nitter/wiki/Instances', { active: true, insert: true }));
GM.registerMenuCommand('Set Bibliogram instance', () => setInstance('bibliogram'));
GM.registerMenuCommand('Show list of known Bibliogram instances', () => GM.openInTab('https://git.sr.ht/~cadence/bibliogram-docs/tree/master/docs/Instances.md', { active: true, insert: true }));
GM.registerMenuCommand('Set Teddit instance', () => setInstance('teddit'));
GM.registerMenuCommand('Show list of known Teddit instances', () => GM.openInTab('https://codeberg.org/teddit/teddit#instances', { active: true, insert: true }));
GM.registerMenuCommand('Set Scribe instance', () => setInstance('scribe'));
GM.registerMenuCommand('Rewrite now', doRewrite);
console.log('Registered menus.');
}
// Toggle Invidious proxy
async function toggleInvidiousProxy() {
let cfg = JSON.parse(await GM.getValue('YT2IConfig', JSON.stringify(defaultConfig)));
cfg.invProxy = cfg.invProxy === 1 ? 0 : 1;
GM.setValue('YT2IConfig', JSON.stringify(cfg));
console.log(`Invidious proxying toggled to ${cfg.invProxy ? 'on' : 'off'}.`);
}
// Register keyboard shortcut Alt+Y to trigger rewrite
document.onkeyup = function (e) {
var e = e || window.event; // for IE to cover IEs window event-object
if (e.altKey && e.which == 89) {
doRewrite();
return false;
}
};
Please ensure you adjust the URLs and any other hard-coded instances as necessary to match the desired replacements for your use case.
Not yet included in a published release.