Skip to content

Commit

Permalink
refactor: addElement, addStyle; remove GM_addStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
PipecraftNet committed Aug 5, 2023
1 parent d0a99b9 commit e945c29
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 19 deletions.
23 changes: 17 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,29 @@ export const $$ = (selectors, element) => [
export const querySelector = $
export const querySelectorAll = $$

export const getRootElement = (type) =>
type === 1
? doc.head || doc.body || doc.documentElement
: type === 2
? doc.body || doc.documentElement
: doc.documentElement

export const createElement = (tagName, attributes) =>
setAttributes(doc.createElement(tagName), attributes)

export const addElement = (parentNode, tagName, attributes) => {
if (!parentNode) {
if (typeof parentNode === "string") {
return addElement(null, parentNode, tagName)
}

if (!tagName) {
return
}

if (typeof parentNode === "string") {
attributes = tagName
tagName = parentNode
parentNode = doc.head
if (!parentNode) {
parentNode = /^(script|link|style|meta)$/.test(tagName)
? getRootElement(1)
: getRootElement(2)
}

if (typeof tagName === "string") {
Expand All @@ -54,7 +65,7 @@ export const addElement = (parentNode, tagName, attributes) => {

export const addStyle = (styleText) => {
const element = createElement("style", { textContent: styleText })
doc.head.append(element)
getRootElement(1).append(element)
return element
}

Expand Down
43 changes: 30 additions & 13 deletions lib/userscript.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {
doc,
getRootElement,
setAttributes,
addElement as _addElement,
addStyle as _addStyle,
} from "./index.js"

export * from "./index.js"
Expand All @@ -24,19 +23,39 @@ process.env.PLASMO_TAG === "dev" &&
export const addElement =
typeof GM_addElement === "function"
? (parentNode, tagName, attributes) => {
if (!parentNode) {
if (typeof parentNode === "string") {
return addElement(null, parentNode, tagName)
}

if (!tagName) {
return
}

if (typeof parentNode === "string") {
attributes = tagName
tagName = parentNode
parentNode = doc.head
if (!parentNode) {
parentNode = /^(script|link|style|meta)$/.test(tagName)
? getRootElement(1)
: getRootElement(2)
}

if (typeof tagName === "string") {
const element = GM_addElement(tagName)
setAttributes(element, attributes)
let attributes2
if (attributes) {
const entries1 = []
const entries2 = []
for (const entry of Object.entries(attributes)) {
if (/^(on\w+|innerHTML)$/.test(entry[0])) {
entries2.push(entry)
} else {
entries1.push(entry)
}
}

attributes = Object.fromEntries(entries1)
attributes2 = Object.fromEntries(entries2)
}

const element = GM_addElement(null, tagName, attributes)
setAttributes(element, attributes2)
parentNode.append(element)
return element
}
Expand All @@ -48,10 +67,8 @@ export const addElement =
}
: _addElement

export const addStyle =
typeof GM_addStyle === "function"
? (styleText) => GM_addStyle(styleText)
: _addStyle
export const addStyle = (styleText) =>
addElement(null, "style", { textContent: styleText })

// Only register menu on top frame
export const registerMenuCommand = (name, callback, accessKey) => {
Expand Down

0 comments on commit e945c29

Please sign in to comment.