Skip to content

Instantly share code, notes, and snippets.

View alejandrocoding's full-sized avatar
🏠
Working from home

Alejandro Lora alejandrocoding

🏠
Working from home
View GitHub Profile
@alejandrocoding
alejandrocoding / contentScript.ts
Last active June 24, 2020 18:11
Content script adding p element to show latest commit
try {
const authorAnchor = document.querySelector('.commit-author') as HTMLAnchorElement;
const commit = (authorAnchor.nextElementSibling as HTMLAnchorElement).href.split('commit/')[1];
const p = document.createElement('p');
p.classList.add('my-commit-msg');
p.innerText = `Latest commit was: ${commit}`;
anchorMyBtn.parentNode.parentNode.prepend(p);
} catch {
@alejandrocoding
alejandrocoding / contentScript.ts
Created June 24, 2020 17:55
Content Script adding button to GitHub UI with link to Google
const anchorMyBtn = document.createElement('a');
anchorMyBtn.innerText = 'MY BUTTON';
anchorMyBtn.classList.add('btn', 'ml-2');
anchorMyBtn.href = 'https://google.es/';
anchorMyBtn.target = '_blank';
@alejandrocoding
alejandrocoding / angular.json5
Created June 24, 2020 17:19
Angular json file adding contentStyle scss to bundle as a separate css file (extracted)
...
"extractCss": true,
"vendorChunk": false,
"assets": [
"src/favicon.ico",
"src/assets",
"src/manifest.json"
],
"styles": [
"src/styles.scss",
@alejandrocoding
alejandrocoding / manifest.json
Created June 24, 2020 17:17
Manifest adding contentStyle css file
"content_scripts": [
{
"matches": ["*://*.github.com/*"],
"js": ["contentScript.js", "runtime.js"],
"css": ["contentStyle.css"]
}
]
@alejandrocoding
alejandrocoding / background.ts
Created June 24, 2020 12:50
Calling addBookMarkFolder & addBookMarkLink from onMessage Listener
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.myBookMark) {
chrome.bookmarks.getTree((bookmarks) => { // let's search and try to find if the folder already exists
const bookmarkBar = bookmarks[0].children.find(bookmark => bookmark.id === '1'); // the bookmar bar is given id '1'
const existAlready = bookmarkBar.children.find(bookmark => bookmark.title === BOOKMARK_FOLDER.title);
if (!existAlready) {
addBookMarkFolder()
.pipe(concatMap((folder) => addBookMarkLink(folder))) // concat to pass the folder created value down to addBookMarkLink
.subscribe((result) => sendResponse(result)); // sending as a response the result
}
@alejandrocoding
alejandrocoding / background.ts
Last active February 26, 2024 07:13
Functions for adding bookmark folders and links by Observables
function addBookMarkFolder(): Observable<chrome.bookmarks.BookmarkTreeNode> {
return new Observable((observer) =>
chrome.bookmarks.create(BOOKMARK_FOLDER, (bookMarkFolder) =>
observer.next(bookMarkFolder)
)
);
}
function addBookMarkLink(bookMarkFolder: chrome.bookmarks.BookmarkTreeNode): Observable<string> {
return new Observable((observer) =>
@alejandrocoding
alejandrocoding / constants.ts
Created June 24, 2020 12:36
Creating constants for bookmark folder and link item
export const BOOKMARK_FOLDER: chrome.bookmarks.BookmarkCreateArg = {
parentId: '1',
title: 'BookMark!',
} as const;
export const BOOKMARK_LINK: chrome.bookmarks.BookmarkCreateArg = {
title: 'My first BookMark from Extension!',
url: 'https://google.es/',
} as const;
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.myBookMark) {
chrome.bookmarks.create(
{
parentId: '1',
title: 'BookMark!',
url: 'https://google.es/'
},
() => sendResponse('I have added my first bookmark!') // This is the response back for the emitter of the message in contentScript.ts
);
@alejandrocoding
alejandrocoding / contentScript.ts
Last active June 24, 2020 12:27
Sending a message from contentScript to access bookmark API
console.log('Starting contentScript');
chrome.runtime.sendMessage({ myBookMark: true }, (response) => {
console.log(response); // this is the response back from the listener in background.ts
});
console.log('Ending contentScript');
@alejandrocoding
alejandrocoding / contentScript.ts
Created June 24, 2020 11:30
Attempt of use forbidden - bookmark API from contentScript
console.log('Starting contentScript');
chrome.bookmarks.create(
{
parentId: null,
title: 'My first BookMark from Extension!'
},
(newFolder) => console.log('added folder: ' + newFolder.title)
);