Skip to content

Instantly share code, notes, and snippets.

@cheadrian
Last active July 15, 2023 21:13
Show Gist options
  • Save cheadrian/d3a6604c0f680980cd232a89a1719153 to your computer and use it in GitHub Desktop.
Save cheadrian/d3a6604c0f680980cd232a89a1719153 to your computer and use it in GitHub Desktop.
Insert Firebase functions inside website using Chrome Extension. Web Version 9 CDN modular Firebase Firestore.
//This file will be injected before body tag.
//Add file to src or modify path accordingly in manifest.json
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.1.2/firebase-app.js"
import { getFirestore, collection, query, where, getDocs } from "https://www.gstatic.com/firebasejs/9.1.2/firebase-firestore.js"
const firebaseConfig = {
apiKey: "XXXXXXXXXXXXXXX",
authDomain: "XXXXXX.firebaseapp.com",
projectId: "XXXXXX",
storageBucket: "XXXXXXX.appspot.com",
messagingSenderId: "XXXXXXXX",
appId: "1:XXXXXXXXXX:web:XXXXXXXXXXXXX"
};
const firebase_app = initializeApp(firebaseConfig);
console.log(firebase_app)
const db = getFirestore(firebase_app);
async function get_database_elements(db_name){
const q = query(collection(db, db_name));
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
console.log(doc.id, " => ", doc.data());
});
}
//In order to use firebase_app and db inside the injected website
//pass to global scope is needed, because in module it has local scope
globalThis.firebase_app = firebase_app;
globalThis.db = db;
globalThis.get_database_elements = get_database_elements;
//This will inject the module.
//Add file to src
function injectModule(file, node) {
var th = document.getElementsByTagName(node)[0];
var s = document.createElement('script');
s.setAttribute('type', 'module');
s.setAttribute('src', file);
th.appendChild(s);
}
//Can be deleted. Use only if you need to inject custom css
function injectStyle(file, node) {
var th = document.getElementsByTagName(node)[0];
var s = document.createElement('link');
s.setAttribute('rel', 'stylesheet');
s.setAttribute('type', 'text/css');
s.setAttribute('href', file);
th.appendChild(s);
}
//Can be deleted. Use only if you need to inject custom Javascript
function injectScript(file, node) {
var th = document.getElementsByTagName(node)[0];
var s = document.createElement('script');
s.setAttribute('type', 'text/javascript');
s.setAttribute('src', file);
th.appendChild(s);
}
function add_base_url(url, node) {
var th = document.getElementsByTagName(node)[0];
var s = document.createElement('base_ext');
s.setAttribute('url', url);
s.setAttribute('ext_id', chrome.runtime.id)
th.appendChild(s);
}
var base_url = chrome.extension.getURL('');
add_base_url(base_url, 'body');
injectModule(chrome.extension.getURL('js/firebase_config.js'), 'body');
{
"comment" : "This is not complete manifest.json, just append what is need for Firebase to work.",
}
"manifest_version": 2,
"content_security_policy": "script-src 'self' https://www.gstatic.com/ https://*.firebaseio.com https://www.googleapis.com https://www.googletagmanager.com; object-src 'self'",
"content_scripts": [
{
"js": [
"src/inject/inject.js"
],
"run_at": "document_idle",
"all_frames": true
}
],
"web_accessible_resources": [
"firebase_config.js"
],
@diegoromerodev
Copy link

Any chance of getting this to work on V3?

@cheadrian
Copy link
Author

Looking forward to update it.
Actually I didn't test with manifest V3.

@cheadrian
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment