Skip to content

Commit

Permalink
v3.3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
adokseo committed Jul 18, 2022
1 parent 94dcd42 commit d748925
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 146 deletions.
40 changes: 22 additions & 18 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,25 +227,29 @@ chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
var action = message.action;

if (action === 'insert-user-agent-stylesheet') {
chrome.scripting.insertCSS({
target: {
tabId: sender.tab.id,
allFrames: true
},
files: [
'/content-scripts/user-agent-stylesheet.css'
]
});
if (!sender.frameId) {
chrome.scripting.insertCSS({
target: {
tabId: sender.tab.id,
allFrames: true
},
files: [
'/content-scripts/user-agent-stylesheet.css'
]
});
}
} else if (action === 'remove-user-agent-stylesheet') {
chrome.scripting.removeCSS({
target: {
tabId: sender.tab.id,
allFrames: true
},
files: [
'/content-scripts/user-agent-stylesheet.css'
]
});
if (!sender.frameId) {
chrome.scripting.removeCSS({
target: {
tabId: sender.tab.id,
allFrames: true
},
files: [
'/content-scripts/user-agent-stylesheet.css'
]
});
}
} else if (action === 'tab-connected') {
sendResponse(new URL(sender.url).hostname);
} else if (action === 'options-page-connected') {
Expand Down
12 changes: 6 additions & 6 deletions content-scripts/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var extension = {
filters: {}
}
},
hostname: location.hostname,
hostname: location.hostname || location.href,
websiteHasDarkTheme: false
};

Expand Down Expand Up @@ -165,11 +165,11 @@ extension.allowTransitions = function () {
};

extension.allowColors = function (value) {
if (value !== false) {
document.documentElement.setAttribute('dm-allow-colors', '');
} else {
document.documentElement.removeAttribute('dm-allow-colors');
}
document.documentElement.setAttribute('dm-allow-colors', '');
};

extension.disallowColors = function (value) {
document.documentElement.removeAttribute('dm-allow-colors');
};

extension.init = function () {
Expand Down
133 changes: 72 additions & 61 deletions content-scripts/dynamic-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extension.dynamicFilter = {
link: [],
style: []
},
status: false,
indexes: [],
styles: {},
elements: {},
Expand Down Expand Up @@ -1019,78 +1020,88 @@ extension.dynamicFilter.scale = function (x, inLow, inHigh, outLow, outHigh) {
--------------------------------------------------------------*/

extension.dynamicFilter.activate = function () {
extension.allowColors(false);

extension.dynamicFilter.queryLinks();
extension.dynamicFilter.queryStyles();
extension.dynamicFilter.queryInlines();

extension.dynamicFilter.observer = new MutationObserver(extension.dynamicFilter.parseMutations);

extension.dynamicFilter.observer.observe(document, {
attributes: true,
attributeFilter: [
'bgcolor',
'color',
'fill',
'stop-color',
'stroke',
'style'
],
childList: true,
subtree: true
});

chrome.runtime.sendMessage({
action: 'insert-user-agent-stylesheet'
});
if (extension.dynamicFilter.status === false) {
extension.dynamicFilter.status = true;

document.documentElement.removeAttribute('dm-default-theme');

extension.disallowColors();

extension.dynamicFilter.queryLinks();
extension.dynamicFilter.queryStyles();
extension.dynamicFilter.queryInlines();

extension.dynamicFilter.observer = new MutationObserver(extension.dynamicFilter.parseMutations);

extension.dynamicFilter.observer.observe(document, {
attributes: true,
attributeFilter: [
'bgcolor',
'color',
'fill',
'stop-color',
'stroke',
'style'
],
childList: true,
subtree: true
});

chrome.runtime.sendMessage({
action: 'insert-user-agent-stylesheet'
});
}
};

extension.dynamicFilter.deactivate = function () {
var elements = {
attribute: document.querySelectorAll('.dark-mode--attribute'),
bgcolor: document.querySelectorAll('.dark-mode--bgcolor'),
color: document.querySelectorAll('.dark-mode--color'),
stylesheet: document.querySelectorAll('.dark-mode--stylesheet')
};

for (var i = 0, l = elements.attribute.length; i < l; i++) {
var element = elements.attribute[i];

element.classList.remove('dark-mode--attribute');
element.setAttribute('style', element.getAttribute('dm-old-style'));
element.removeAttribute('dm-old-style');
}
if (extension.dynamicFilter.status === true) {
extension.dynamicFilter.status = false;

var elements = {
attribute: document.querySelectorAll('.dark-mode--attribute'),
bgcolor: document.querySelectorAll('.dark-mode--bgcolor'),
color: document.querySelectorAll('.dark-mode--color'),
stylesheet: document.querySelectorAll('.dark-mode--stylesheet')
};

for (var i = 0, l = elements.attribute.length; i < l; i++) {
var element = elements.attribute[i];

element.classList.remove('dark-mode--attribute');
element.setAttribute('style', element.getAttribute('dm-old-style'));
element.removeAttribute('dm-old-style');
}

for (var i = 0, l = elements.bgcolor.length; i < l; i++) {
var element = elements.bgcolor[i];
for (var i = 0, l = elements.bgcolor.length; i < l; i++) {
var element = elements.bgcolor[i];

element.classList.remove('dark-mode--bgcolor');
element.setAttribute('bgcolor', element.getAttribute('dm-old-bgcolor'));
element.removeAttribute('dm-old-bgcolor');
}
element.classList.remove('dark-mode--bgcolor');
element.setAttribute('bgcolor', element.getAttribute('dm-old-bgcolor'));
element.removeAttribute('dm-old-bgcolor');
}

for (var i = 0, l = elements.color.length; i < l; i++) {
var element = elements.color[i];
for (var i = 0, l = elements.color.length; i < l; i++) {
var element = elements.color[i];

element.classList.remove('dark-mode--color');
element.setAttribute('color', element.getAttribute('dm-old-color'));
element.removeAttribute('dm-old-color');
}
element.classList.remove('dark-mode--color');
element.setAttribute('color', element.getAttribute('dm-old-color'));
element.removeAttribute('dm-old-color');
}

for (var i = 0, l = elements.stylesheet.length; i < l; i++) {
elements.stylesheet[i].remove();
}
for (var i = 0, l = elements.stylesheet.length; i < l; i++) {
elements.stylesheet[i].remove();
}

if (extension.dynamicFilter.observer) {
extension.dynamicFilter.observer.disconnect();
if (extension.dynamicFilter.observer) {
extension.dynamicFilter.observer.disconnect();

delete extension.dynamicFilter.observer;
}
delete extension.dynamicFilter.observer;
}

chrome.runtime.sendMessage({
action: 'remove-user-agent-stylesheet'
});
chrome.runtime.sendMessage({
action: 'remove-user-agent-stylesheet'
});
}
};


Expand Down
111 changes: 51 additions & 60 deletions content-scripts/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,14 @@ extension.filters = function (changed) {
} else {
html.removeAttribute('dm-invert-colors');

if (theme === 'dynamic' && extension.websiteHasDarkTheme === false && !extension.dynamicFilter.observer) {
if (theme === 'dynamic' && extension.websiteHasDarkTheme === false) {
extension.dynamicFilter.activate();
} else {
extension.allowColors();
extension.dynamicFilter.deactivate();
}
}

if (changed === true && theme !== 'dynamic') {
extension.dynamicFilter.deactivate();
}

if (bluelight > 0) {
style += ' url(#dark-mode__bluelight-filter)';
}
Expand Down Expand Up @@ -138,51 +135,33 @@ extension.filters = function (changed) {
--------------------------------------------------------------*/

extension.checkDefaultTheme = function () {
if (localStorage && localStorage.getItem('dm-default-theme') === 'dark') {
document.documentElement.setAttribute('dm-default-theme', 'dark');

extension.websiteHasDarkTheme = true;

return;
}

if (
extension.storage.website.theme === 'dynamic' &&
extension.websiteHasDarkTheme === false
) {
document.documentElement.removeAttribute('dm-default-theme');

extension.websiteHasDarkTheme = false;

return;
}
if (extension.dynamicFilter.status === false) {
var colors = [],
is_dark = false,
allowed_colors = document.documentElement.hasAttribute('dm-allow-colors');

var colors = [],
is_dark = false;

extension.allowColors(false);
extension.allowColors();

function parse(element, depth, depth_limit) {
depth++;
function parse(element, depth, depth_limit) {
depth++;

for (var i = 0, l = element.children.length; i < l; i++) {
var child = element.children[i],
rect = child.getBoundingClientRect();
for (var i = 0, l = element.children.length; i < l; i++) {
var child = element.children[i],
rect = child.getBoundingClientRect();

if (
rect.width >= document.body.offsetWidth &&
rect.height >= window.innerHeight
) {
colors.push(satus.css(child, 'background-color'));
}
if (
rect.width >= document.body.offsetWidth &&
rect.height >= window.innerHeight
) {
colors.push(satus.css(child, 'background-color'));
}

if (depth < depth_limit && child.children) {
parse(child, depth, depth_limit);
if (depth < depth_limit && child.children) {
parse(child, depth, depth_limit);
}
}
}
}

setTimeout(function () {
colors.push(satus.css(document.documentElement, 'background-color'));
colors.push(satus.css(document.body, 'background-color'));

Expand Down Expand Up @@ -214,39 +193,51 @@ extension.checkDefaultTheme = function () {
}

extension.websiteHasDarkTheme = true;
} else {
localStorage.removeItem('dm-default-theme');
}

extension.allowColors();
});
if (allowed_colors === false) {
extension.disallowColors();
}

console.log(extension.hostname, 'default-dark', extension.websiteHasDarkTheme);
}
};

extension.events.on('extension-loaded', function () {
extension.checkDefaultTheme();

if (localStorage && localStorage.getItem('dm-default-theme') === 'dark') {
document.documentElement.setAttribute('dm-default-theme', 'dark');

extension.websiteHasDarkTheme = true;

extension.allowColors();
}

extension.filters();
});

extension.events.on('website-loaded', function () {
extension.checkDefaultTheme();
if (extension.ready > 2) {
extension.checkDefaultTheme();

extension.filters();
extension.filters();
}
});

extension.events.on('storage-changed', function () {
extension.filters(true);
if (extension.ready > 2) {
extension.filters(true);
}
});

window.addEventListener('focus', function () {
setTimeout(function () {
extension.checkDefaultTheme();
extension.filters();
}, 125);
});
if (extension.ready > 2) {
setTimeout(function () {
extension.checkDefaultTheme();
extension.filters();
}, 125);
}
});

if (localStorage && localStorage.getItem('dm-default-theme') === 'dark') {
document.documentElement.setAttribute('dm-default-theme', 'dark');

extension.websiteHasDarkTheme = true;

extension.allowColors();
}
Loading

0 comments on commit d748925

Please sign in to comment.