JavaScript Code
var str = "hi";
Memory allocation:
Address | Value | Description |
---|---|---|
...... |
... |
#!/usr/bin/groovy | |
/* | |
* Copyright (c) 2016, Andrey Makeev <[email protected]> | |
* 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 |
JavaScript Code
var str = "hi";
Memory allocation:
Address | Value | Description |
---|---|---|
...... |
... |
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. | |
*/ |
export interface Message<T> { | |
type: string | |
subtype: string | |
data: T | |
} | |
export interface Response<V> { | |
status: number | |
data: V | |
} |
// 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) |
// 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" |
// 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) | |
} | |
}) | |
}) |
function onAuthSuccess(user: User) { | |
// Initialise your client environment after authentication | |
//... | |
setLanguageCookie(user.language) | |
i18n.changeLanguage(user.language) | |
} | |
// ... | |
function onUserLanguageChange(language: LanguagesSupported) { | |
// ... | |
setLanguageCookie(language) |