Skip to content
This repository was archived by the owner on Dec 28, 2023. It is now read-only.

Commit 8206d3a

Browse files
committed
v1.0.1
use Shadow DOM to isolate CSS injection
1 parent 22d4394 commit 8206d3a

File tree

8 files changed

+41
-15
lines changed

8 files changed

+41
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![spc-screenshot](screenshot.png)
44

5-
I use special characters frequently and I'm a dev.
5+
I use special characters here and there frequently, so there is this.
66

77
## Installation
88

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "spc",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "A browser extension to insert special characters.",
55
"repository": "https://github.com/khang-nd/spc",
66
"license": "MIT",

public/manifest.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 2,
33
"name": "SPC",
44
"description": "Inserting special characters made easy.",
5-
"version": "1.0.0",
5+
"version": "1.0.1",
66
"homepage_url": "https://github.com/khang-nd/spc",
77
"permissions": ["contextMenus", "activeTab"],
88
"icons": { "48": "icon.png" },
@@ -13,11 +13,12 @@
1313
"content_scripts": [
1414
{
1515
"matches": ["<all_urls>"],
16-
"css": ["build/main.css"]
16+
"js": ["build/main.css.js"]
1717
}
1818
],
19+
"web_accessible_resources": ["build/main.css"],
1920
"commands": {
20-
"open-popup": {
21+
"open_popup": {
2122
"description": "Activate the extension",
2223
"suggested_key": {
2324
"default": "Alt+Period"

rollup.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function buildConfig(fileName, plugins = []) {
4040
name: fileName,
4141
format: "iife",
4242
sourcemap: false,
43+
compact: true,
4344
file: `public/build/${fileName}`,
4445
},
4546
plugins: [resolve(), commonjs(), ...plugins],
@@ -48,6 +49,7 @@ function buildConfig(fileName, plugins = []) {
4849

4950
export default [
5051
buildConfig("background.js"),
52+
buildConfig("main.css.js"),
5153
buildConfig("main.js", [
5254
svelte({
5355
compilerOptions: { dev: !production },

src/Popup.svelte

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import { onMount } from "svelte";
44
import { Tabs, Tab, TabContent } from "svelte-materialify/dist";
55
import { isFormElement } from "./utils";
6+
import { popup } from "./identifiers";
67
78
/** @type {Document} */
89
export let document;
9-
export let id;
1010
export let offset;
1111
1212
const { activeElement } = document;
@@ -29,14 +29,20 @@
2929
self.remove();
3030
}
3131
32+
function dismissWithESC({ key }) {
33+
if (key === "Escape") dismiss();
34+
}
35+
3236
function stopPropagation(event) {
3337
event.stopPropagation();
3438
}
3539
3640
onMount(() => {
3741
document.addEventListener("click", dismiss);
3842
self.addEventListener("click", stopPropagation);
43+
self.addEventListener("keyup", dismissWithESC);
3944
activeElement.addEventListener("click", stopPropagation);
45+
activeElement.addEventListener("keyup", dismissWithESC);
4046
});
4147
4248
$: if (searchText) {
@@ -49,7 +55,7 @@
4955
}
5056
</script>
5157

52-
<main bind:this={self} {id} style="top: {offset.y}px; left: {offset.x}px">
58+
<main bind:this={self} id={popup} style="top: {offset.y}px; left: {offset.x}px">
5359
<header>
5460
<input type="search" placeholder="Search..." bind:value={searchText} />
5561
<button aria-label="Close" on:click={dismiss}>✕</button>
@@ -73,11 +79,8 @@
7379
</main>
7480

7581
<style>
76-
:root {
77-
--spacing: 0.5em;
78-
}
79-
8082
#spc {
83+
--spacing: 0.5em;
8184
z-index: 9999;
8285
position: absolute;
8386
background: #f8f8f8;

src/identifiers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const wrapper = "spc-wrapper";
2+
export const popup = "spc";

src/main.css.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// inject CSS to the Shadow DOM
2+
// to avoid CSS collision with the main DOM
3+
4+
import browser from "webextension-polyfill";
5+
import { wrapper as id } from "./identifiers";
6+
7+
window.addEventListener("load", () => {
8+
const wrapper = document.createElement("div");
9+
const link = document.createElement("link");
10+
const shadow = wrapper.attachShadow({ mode: "open" });
11+
12+
wrapper.id = id;
13+
link.rel = "stylesheet";
14+
link.href = browser.extension.getURL("build/main.css");
15+
shadow.append(link);
16+
document.body.append(wrapper);
17+
});

src/main.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import Popup from "./Popup.svelte";
22
import { isEditable } from "./utils";
3+
import * as id from "./identifiers";
34

4-
const id = "spc";
55
const { activeElement } = document;
66
let popup = null;
77

88
if (isEditable(activeElement)) {
9+
const { shadowRoot } = document.querySelector("#" + id.wrapper);
910
const { top, left, height } = activeElement.getBoundingClientRect();
1011
const x = left + 3;
1112
const y = top + height;
1213

13-
popup = document.querySelector("#" + id);
14+
popup = shadowRoot.querySelector("#" + id.popup);
1415
popup && popup.remove();
1516
popup = new Popup({
16-
target: document.body,
17-
props: { document, id, offset: { x, y } },
17+
target: shadowRoot,
18+
props: { document, offset: { x, y } },
1819
});
1920
}
2021

0 commit comments

Comments
 (0)