Skip to content

Instantly share code, notes, and snippets.

View JohnRSim's full-sized avatar

John Sim JohnRSim

View GitHub Profile
This file has been truncated, but you can view the full file.
var WebComponentDemo=function(L){"use strict";var RZ=Object.defineProperty;var yZ=(L,C,B)=>C in L?RZ(L,C,{enumerable:!0,configurable:!0,writable:!0,value:B}):L[C]=B;var s=(L,C,B)=>yZ(L,typeof C!="symbol"?C+"":C,B);function C(){}function B(Z,l){for(const b in l)Z[b]=l[b];return Z}function yl(Z){return Z()}function ul(){return Object.create(null)}function v(Z){Z.forEach(yl)}function bl(Z){return typeof Z=="function"}function E(Z,l){return Z!=Z?l==l:Z!==l||Z&&typeof Z=="object"||typeof Z=="function"}function xl(Z){return Object.keys(Z).length===0}function dl(Z,l,b,d){if(Z){const X=Yl(Z,l,b,d);return Z[0](X)}}function Yl(Z,l,b,d){return Z[1]&&d?B(b.ctx.slice(),Z[1](d(l))):b.ctx}function Xl(Z,l,b,d){if(Z[2]&&d){const X=Z[2](d(b));if(l.dirty===void 0)return X;if(typeof X=="object"){const m=[],c=Math.max(l.dirty.length,X.length);for(let G=0;G<c;G+=1)m[G]=l.dirty[G]|X[G];return m}return l.dirty|X}return l.dirty}function ml(Z,l,b,d,X,m){if(X){const c=Yl(l,b,d,m);Z.p(c,X)}}function cl(Z){if(Z.ctx.length>32){const l=[],
// Initialize the web worker
const worker = new Worker('translationWorker.js');
// Listen for messages from the worker
worker.addEventListener('message', (event) => {
if (event.data.status === 'update') {
console.log('Partial output:', event.data.output);
} else if (event.data.status === 'complete') {
console.log('Translation complete:', event.data.output);
} else {
import { pipeline } from '@xenova/transformers';
/**
* This class uses the Singleton pattern to ensure that only one instance of the
* pipeline is loaded. This is because loading the pipeline is an expensive
* operation and we don't want to do it every time we want to translate a sentence.
*/
class MyTranslationPipeline {
static task = 'translation';
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.6.0';
env.allowLocalModels = false;
const status = document.getElementById('status');
status.textContent = 'Loading model...';
const translator = await pipeline('translation', 'Xenova/nllb-200-distilled-600M');
status.textContent = 'Ready';
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Translation Manager</title>
<style>
/* Modal styles */
.modal {
display: none;
// Retrieve translations from local storage or use default
const storedTranslations = localStorage.getItem('translations');
const translations = storedTranslations ? JSON.parse(storedTranslations) : {
"Hello, world!": "Hola, mundo!",
"This is a test.": "Esto es una prueba.",
// Add more default translations as needed
};
// Function to update the translations object with new strings that need translating
function updateTranslations(textNodes, translations) {
textNodes.forEach(node => {
const originalText = node.nodeValue.trim();
if (!translations.hasOwnProperty(originalText)) {
translations[originalText] = "";
}
});
}
@JohnRSim
JohnRSim / page_text_scraper.js
Last active July 11, 2024 09:18
Crawl & replace generated text with static translation
// Function to collect all text nodes
function getTextNodes(element) {
let textNodes = [];
function recursiveCollect(node) {
if (node.nodeType === Node.TEXT_NODE) {
// Check if the node value is not blank or just newline characters
if (node.nodeValue.trim() !== "") {
textNodes.push(node);
}
} else {
@JohnRSim
JohnRSim / gist:d9b7d8a94c2966b92134b5c5b4f65e92
Created January 2, 2024 12:36
CEC Transfer-Content CMD Line example
cec tc MyDemoRepository -s BitmapBytesServer -d BitmapBytesServer -n 1000
@JohnRSim
JohnRSim / OCM_cacheBusting.js
Last active June 28, 2023 16:11
Example of applying CDN Cache Busting support for OCM Images
/**
* Cachebusting value based on update date from OCM Asset to reset the cache
*/
function cb(asset) {
const time = new Date(asset.updatedDate.value).getTime()
return parseInt(time, 10).toString(36)
}
/**
* Retrieve the sourceset for an asset that is constructed from the rendition