Skip to content

Commit

Permalink
Fixed bugs with hiding body before blur processing.
Browse files Browse the repository at this point in the history
  • Loading branch information
getvictor committed May 27, 2024
1 parent 4ca6157 commit 6d52a7f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 25 deletions.
41 changes: 41 additions & 0 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { MODES } from "./constants"

const cssToInject = "body { visibility: hidden; }"

let currentModeIndex = 1

console.debug("OpenBlur service worker script loaded")

function startUp() {
chrome.storage.sync.get("mode", (data) => {
if (data.mode) {
Expand All @@ -15,3 +19,40 @@ function startUp() {
// Ensure the background script always runs.
chrome.runtime.onStartup.addListener(startUp)
chrome.runtime.onInstalled.addListener(startUp)

// Hide the body content until OpenBlur processes the content and blurs the secrets.
chrome.webNavigation.onCommitted.addListener(
function(details) {
if (MODES[currentModeIndex].id === "on") {
let target: chrome.scripting.InjectionTarget = {
tabId: details.tabId,
frameIds: [details.frameId],
}
void chrome.scripting.insertCSS({
css: cssToInject,
target: target,
})
}
}
)

// Unhide the body content.
chrome.runtime.onMessage.addListener(
function(request, sender, _sendResponse) {
if (sender.tab && request.action === "unhideBody") {
let target: chrome.scripting.InjectionTarget = {
tabId: sender.tab.id!,
}
if (sender.frameId !== undefined) {
target.frameIds = [sender.frameId]
}
void chrome.scripting.removeCSS({
css: cssToInject,
target: target,
})
}
if (request.mode) {
currentModeIndex = request.mode.index
}
}
)
30 changes: 17 additions & 13 deletions src/content.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { NUMBER_OF_ITEMS} from "./constants"

const blurFilter = "blur(0.343em)" // This unique filter value identifies the OpenBlur filter.
const tagsNotToBlur = ["SCRIPT", "STYLE"]
const tagsNotToBlur = ["SCRIPT", "STYLE", "loc"]

let contentToBlur: string[] = []
let enabled = true
let bodyHidden = true

console.debug("OpenBlur content script loaded")

function unhideBody() {
document.body.style.visibility = "visible"
if (bodyHidden) {
void chrome.runtime.sendMessage({action: "unhideBody"})
bodyHidden = false
}
}

function processNode(node: Node) {
Expand All @@ -18,7 +22,7 @@ function processNode(node: Node) {
}
if (node.nodeType === Node.TEXT_NODE && node.textContent !== null && node.textContent.trim().length > 0) {
const parent = node.parentElement
if (parent !== null) {
if (parent !== null && parent.style) {
if (tagsNotToBlur.includes(parent.tagName)) {
return
} else if (parent.style.filter.includes(blurFilter)) {
Expand All @@ -29,16 +33,16 @@ function processNode(node: Node) {
}
return
}
}
const text = node.textContent!
if (enabled) {
contentToBlur.some((content) => {
if (text.includes(content)) {
blurElement(parent!)
return true
}
return false
})
const text = node.textContent!
if (enabled) {
contentToBlur.some((content) => {
if (text.includes(content)) {
blurElement(parent!)
return true
}
return false
})
}
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions static/content-start.css

This file was deleted.

12 changes: 4 additions & 8 deletions static/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@
"default_popup": "popup.html"
},
"permissions": [
"storage"
"scripting",
"storage",
"webNavigation"
],
"host_permissions": ["<all_urls>"],
"content_scripts": [
{
"matches": ["<all_urls>"],
"all_frames": true,
"match_origin_as_fallback": true,
"css": ["content-start.css"],
"run_at": "document_start"
},
{
"matches": ["<all_urls>"],
"all_frames": true,
Expand Down

0 comments on commit 6d52a7f

Please sign in to comment.