Skip to content

Instantly share code, notes, and snippets.

View MariusBongarts's full-sized avatar

Marius Bongarts MariusBongarts

View GitHub Profile
@MariusBongarts
MariusBongarts / manifest.json
Last active September 27, 2021 10:59
Medium Highlighter - manifest.json 2
{
"name": "Background Color Changer",
"version": "1.0.0",
"manifest_version": 3,
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": [
"content.js"
]
@MariusBongarts
MariusBongarts / content.js
Last active July 26, 2021 08:26
content.js
alert('Hello world from content.js');
@MariusBongarts
MariusBongarts / medium-highlighter.js
Last active July 26, 2021 08:26
medium-highlighter.js
alert('Hello world from medium-highlighter.js');
@MariusBongarts
MariusBongarts / medium-highlighter.js
Created July 30, 2021 20:08
MediumHighlighter - Template of Web Component
const highlightColor = "rgb(213, 234, 255)";
const template = `
<template id="highlightTemplate">
<span class="highlight" style="background-color: ${highlightColor}; display: inline"></span>
</template>
<button id="mediumHighlighter">
<svg class="text-marker" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 544 512"><path d="M0 479.98L99.92 512l35.45-35.45-67.04-67.04L0 479.98zm124.61-240.01a36.592 36.592 0 0 0-10.79 38.1l13.05 42.83-50.93 50.94 96.23 96.23 50.86-50.86 42.74 13.08c13.73 4.2 28.65-.01 38.15-10.78l35.55-41.64-173.34-173.34-41.52 35.44zm403.31-160.7l-63.2-63.2c-20.49-20.49-53.38-21.52-75.12-2.35L190.55 183.68l169.77 169.78L530.27 154.4c19.18-21.74 18.15-54.63-2.35-75.13z"></path></svg>
</button>
@MariusBongarts
MariusBongarts / medium-highlighter.js
Created July 30, 2021 20:25
MediumHighlighter - styling
const styled = ({ display = "none", left = 0, top = 0 }) => `
#mediumHighlighter {
align-items: center;
background-color: black;
border-radius: 5px;
border: none;
cursor: pointer;
display: ${display};
justify-content: center;
left: ${left}px;
@MariusBongarts
MariusBongarts / medium-highlighter.js
Last active March 19, 2024 14:02
MediumHighlighter - Custom element
class MediumHighlighter extends HTMLElement {
get markerPosition() {
return JSON.parse(this.getAttribute("markerPosition") || "{}");
}
get styleElement() {
return this.shadowRoot.querySelector("style");
}
get highlightTemplate() {
@MariusBongarts
MariusBongarts / content.js
Last active July 30, 2021 21:13
MediumHiglighter - content.js
const mediumHighlighter = document.createElement("medium-highlighter");
document.body.appendChild(mediumHighlighter);
const setMarkerPosition = (markerPosition) =>
mediumHighlighter.setAttribute(
"markerPosition",
JSON.stringify(markerPosition)
);
setMarkerPosition({
@MariusBongarts
MariusBongarts / manifest.json
Created July 30, 2021 21:33
MediumHighlighter - polyfills
"js": [
"node_modules/@webcomponents/custom-elements/custom-elements.min.js",
"medium-highlighter.js",
"content.js"
]
@MariusBongarts
MariusBongarts / content.js
Last active July 30, 2021 21:45
MediumHighlighter - content.js final
const getSelectedText = () => window.getSelection().toString();
document.addEventListener("click", () => {
if (getSelectedText().length > 0) {
setMarkerPosition(getMarkerPosition());
}
});
document.addEventListener("selectionchange", () => {
if (getSelectedText().length === 0) {
@MariusBongarts
MariusBongarts / ProductService.ts
Last active August 8, 2021 15:29
DRY article - ProductService
interface User {
id: string;
isAdmin: boolean;
}
interface Product {
id: string;
title: string;
}