Skip to content

Instantly share code, notes, and snippets.

View EduardoAC's full-sized avatar

Eduardo Aparicio Cardenes EduardoAC

View GitHub Profile
@EduardoAC
EduardoAC / Jenkinsfile
Created April 4, 2017 08:08 — forked from amaksoft/Jenkinsfile
My example Jenkins Pipeline setup for Android app project
#!/usr/bin/groovy
/*
* Copyright (c) 2016, Andrey Makeev <amaksoft@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
@EduardoAC
EduardoAC / jsmem.md
Created July 11, 2017 14:53 — forked from justinbmeyer/jsmem.md
JS Memory

JavaScript Code

var str = "hi";

Memory allocation:

Address Value Description
...... ...
@EduardoAC
EduardoAC / concurrency.handler.ts
Last active September 5, 2023 14:06
Handling concurrent access to resource
type PendingRequests = { [url: string]: Function[] }
// Initialize an object to track pending resource requests
let pendingRequests: PendingRequests = {};
/**
* Check if there is a pending request for a given URL.
* @param {string} url - The URL to check for pending requests.
* @returns {boolean} - True if there are pending requests, false otherwise.
*/
@EduardoAC
EduardoAC / sendMessage.ts
Created November 5, 2023 09:06
Chrome Extension - Send Message Promise Wrapper, designed for streamlined communication with the background script in Manifest v3.
export interface Message<T> {
type: string
subtype: string
data: T
}
export interface Response<V> {
status: number
data: V
}
@EduardoAC
EduardoAC / chromeRuntimeOnMessageListener.ts
Last active November 5, 2023 09:46
Chrome Extension - Runtime onMessage Listener, meticulously crafted for capturing messages exchanged between content and background scripts in the context of Manifest v3.
// Message and response are define in https://gist.github.com/EduardoAC/000b1e39a6ec10a892e7c6cd93730a53
chrome.runtime.onMessage.addListener((message: Message<T>, sender, sendResponse) => {
if(sender.tab) { // Sender Tab useful mostly for background script
switch(message.type) {
case "review":
// Tab is useful for instance to obtain the url to fetch the review from
reviewHandler(sender.tab, message, sendResponse)
break
case "language":
languageHandler(sender.tab, message, sendMessage)
@EduardoAC
EduardoAC / chromeRuntimeOnMessageListenerWithReactContextStore.ts
Last active November 5, 2023 10:09
React Chrome Extension: Combining React Context with Runtime onMessage Listener to capture state updates via messages exchanged between content and background scripts in Manifest v3.
// Message and response are define in https://gist.github.com/EduardoAC/000b1e39a6ec10a892e7c6cd93730a53
interface GlobalContext {
review: number
language: string
setLanguage: Function
}
const globalContext = createContext<GlobalContext>({
review: -1
language: "en"
@EduardoAC
EduardoAC / broadcastingMessageAllLoadedTabs.ts
Created November 5, 2023 10:12
Chrome Extension: Broadcasting a message to all tabs currently loaded in the browser.
// Message and response are define in https://gist.github.com/EduardoAC/000b1e39a6ec10a892e7c6cd93730a53
export function broadcastMessageAllLoadedTabs<T>(message: Message<T>) {
// Get all tabs not discarded - it can be optimise further
chrome.tabs.query({ discarded: false }, (tabs) => {
tabs.forEach(({ id, status }) => {
if(status !== "unloaded") {
sendMessage(id, message)
}
})
})
@EduardoAC
EduardoAC / setLanguageCookie.ts
Created February 11, 2024 08:02
Persist Language in a client-side cookie using js-cookie library
import Cookies from "js-cookie"
const languageCookieKey = "{your-webApp-name}-language"
const cookieOptions = {
// ...
// Only set secure if we are running on an https environment.
// secure: true => force cookie to be only readable by https requests.
secure: window?.location?.protocol === "https:",
}
@EduardoAC
EduardoAC / exampleSettingLanguageWebApp.ts
Created February 11, 2024 08:11
Example setting up language on the Web App allowing real-time sync with the browser extension
function onAuthSuccess(user: User) {
// Initialise your client environment after authentication
//...
setLanguageCookie(user.language)
i18n.changeLanguage(user.language)
}
// ...
function onUserLanguageChange(language: LanguagesSupported) {
// ...
setLanguageCookie(language)
@EduardoAC
EduardoAC / cookieInitInServiceWorker.ts
Last active February 11, 2024 08:27
Example retrieving language from client-side cookie when initialising service worker
try {
const languageCookieFromWebApp: chrome.cookies.Cookie | null =
await chrome.cookies.get({
url: `${your - webApp - url}`, // Remember taht URL change for localhost and live
name: "{your-webApp-name}-language",
});
if (languageCookieFromWebApp) {
const language = languageCookieFromWebApp.value
if (language) {
chrome.storage.local.set({ language });