Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
chrome.action.onClicked.addListener(function(tab)
{
{
chrome.tabs.query({active: true, currentWindow: true}, function(tabs)
{
chrome.tabs.sendMessage(tabs[0].id, {msg: "click"});
Expand All @@ -23,4 +23,26 @@ chrome.runtime.onMessage.addListener(function(request)
chrome.action.setTitle({title:'Měním pozadí a text v e-mailech na černobílo 😉'});
chrome.action.setIcon({path: 'gmailStyle.png'});
}
});
});

function injectScript(tabId) {
chrome.scripting.executeScript(
{
target: {tabId: tabId},
files: ["smartMode.js"]
}
);

}

// adds a listener to tab change
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {

// check for a URL in the changeInfo parameter (url is only added when it is changed)
if (changeInfo.url) {

// calls the inject function
injectScript(tabId);

}
});
2 changes: 1 addition & 1 deletion gmailStyle.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.body.apply-styles {
color: #000;
background-color: #fff;
}
}
2 changes: 1 addition & 1 deletion grayText.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.body.apply-styles {
color: #898989;
}
}
18 changes: 9 additions & 9 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
"description": "Měním nečitelné písmo při použití tmavých témat na Seznam Emailu na čitelné.",
"version": "1.2.1",
"icons": { "128": "grayText.png" },

"permissions":
[
"storage"
"storage", "scripting"
],

"host_permissions":
[
"https://email.seznam.cz/*"
],

"background":
{
"service_worker": "background.js"
},

"action":
{
"default_icon": "inactive.png",
"default_title": "Teď jsem tu jen na ozdobu..."
},

"content_scripts":
[
{
Expand All @@ -35,10 +35,10 @@
"run_at": "document_idle"
}
],
"web_accessible_resources":

"web_accessible_resources":
[{
"resources": ["grayText.css", "gmailStyle.css", "gmailStyle.png"],
"matches": ["https://email.seznam.cz/*"]
}]
}
}
16 changes: 8 additions & 8 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ chrome.runtime.onMessage.addListener(function(request)
if(mode == 0)
{
load("grayText");
}
}
else if(mode == 1)
{
unload("grayText");
Expand All @@ -27,12 +27,12 @@ function loadMode()
chrome.storage.local.get("mod", function (result)
{
mode = result.mod;

if(mode == 2)
{
load("gmailStyle");
}
else if(mode == 1 || mode != 0)
load("gmailStyle");
}
else if(mode == 1 || mode != 0)
{
load("grayText");
}
Expand All @@ -57,7 +57,7 @@ function load(file)
link.type = "text/css";
link.rel = "stylesheet";
document.getElementsByTagName("head")[0].appendChild(link);

if(file == "grayText")
{
mode = 1;
Expand All @@ -66,7 +66,7 @@ function load(file)
{
mode = 2;
}

save(mode);
}

Expand All @@ -82,4 +82,4 @@ function save(mode)
{
chrome.storage.local.set({"mod": mode});
sendMsg(mode);
}
}
114 changes: 114 additions & 0 deletions smartMode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
setContrastMode()

function getLuminance (rgb) {
const [rr, gg, bb] = getRGB(rgb).map(c => {
c /= 255;
return c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
});
return 0.2126 * rr + 0.7152 * gg + 0.0722 * bb;
}

function getRGB(color) {
const rgb = color.match(/\d+/g);
return rgb ? rgb.map(Number) : [0, 0, 0];
}

function getContrast(rgb1, rgb2) {
const lum1 = getLuminance(rgb1);
const lum2 = getLuminance(rgb2);
return (Math.max(lum1, lum2) + 0.05) / (Math.min(lum1, lum2) + 0.05);
}

function getBrightness(rgb) {
const [r, g, b] = getRGB(rgb);
return Math.round(((r * 299) + (g * 587) + (b * 114)) / 1000);
}

function setContrastMode() {
const root = document.querySelector(".body.apply-styles")
if (root == null) {
return;
}

setContrastRecursion(root);
}

function setContrastRecursion(node, bgColor = 'transparent') {
if (node.style.backgroundColor !== "" || node.style.color !== "") {
console.log("element " + node.tagName + " má explicitní barvu, kontrastím...")
const fontColor = window.getComputedStyle(node).color;
bgColor = getBackgroundColor(node, bgColor);
console.log("font má barvu " + fontColor + ", pozadí " + bgColor);

if (getContrast(fontColor, bgColor) < 2.5) {
console.log("element " + node.tagName + " se nedá přečíst, kontrast: " + getContrast(fontColor, bgColor))
setNodeTextColor(node, fontColor, bgColor)
}
}

Array.from(node.children).forEach(child => {
setContrastRecursion(child, bgColor);
})
}

function setNodeTextColor(node, fontColor, bgColor) {
if (fontColor === 'rgb(0, 0, 0)') {
node.style.removeProperty("color");
return;
} else if (fontColor === 'rgb(255, 255, 255)') {
node.style.color = shade(fontColor, 1);
return;
}

if (getBrightness(bgColor) > 125) {
node.style.color = shade(fontColor);
} else {
node.style.color = tint(fontColor);
}

console.log("element " + node.tagName + " se nedá přečíst, měním z " + fontColor + " na " + node.style.color)
}

function tint(rgb, tint_factor = 0.2) {
const [r, g, b] = getRGB(rgb)
console.log("moc tmavé, tintím " + `rgb(${r}, ${g}, ${b}), factor: ${tint_factor}`);
const newR = r + (255 - r) * tint_factor
const newG = g + (255 - g) * tint_factor
const newB = b + (255 - b) * tint_factor
return `rgb(${newR}, ${newG}, ${newB})`;
}

function shade(rgb, shade_factor = 0.2) {
const [r, g, b] = getRGB(rgb)
console.log("moc světlé, šejdím " + `rgb(${r}, ${g}, ${b}), factor: ${shade_factor}`);
const newR = r * (1 - shade_factor)
const newG = g * (1 - shade_factor)
const newB = b * (1 - shade_factor)
return `rgb(${newR}, ${newG}, ${newB})`;
}

function getBackgroundColorFromAncestors(node) {
let bgColor = 'transparent'

while (bgColor === 'rgba(0, 0, 0, 0)' || bgColor === 'transparent') {
node = node.parentElement;
if (!node) return 'rgba(18,18,18)';
bgColor = window.getComputedStyle(node).backgroundColor;
}

return bgColor;
}

function getBackgroundColor(node, inheritedBg) {
let bgColor = window.getComputedStyle(node).backgroundColor;

if (bgColor !== 'rgba(0, 0, 0, 0)' && bgColor !== 'transparent') {
return bgColor;
}

if (inheritedBg !== 'transparent') {
return inheritedBg;
}

return getBackgroundColorFromAncestors(node);
}