feat: enhance site detection and popup functionality
This commit is contained in:
@@ -1,52 +1,41 @@
|
||||
const domain = window.location.hostname;
|
||||
|
||||
chrome.storage.local.get([domain], (result) => {
|
||||
if (result[domain] !== 'disabled') {
|
||||
|
||||
let siteMatched = false;
|
||||
chrome.storage.local.get([domain, `${domain}_mode`], (result) => {
|
||||
if (result[domain] === 'disabled') return;
|
||||
|
||||
const forceGlobal = result[`${domain}_mode`] === 'global';
|
||||
let siteMatched = false;
|
||||
|
||||
if (!forceGlobal) {
|
||||
// 1. SONARR
|
||||
const isSonarr = getComputedStyle(document.documentElement).getPropertyValue('--sonarrBlue').trim() !== "" ||
|
||||
document.title.toLowerCase().includes('sonarr');
|
||||
|
||||
if (isSonarr) {
|
||||
injectSiteStyle('sites/sonarr.local/styles.css');
|
||||
siteMatched = true;
|
||||
}
|
||||
if (isSonarr) { injectSiteStyle('sites/sonarr.local/styles.css'); siteMatched = true; }
|
||||
|
||||
// 2. CHESS.COM
|
||||
const isChess = domain.includes('chess.com') || !!document.querySelector('.board-layout-main');
|
||||
|
||||
if (isChess) {
|
||||
injectSiteStyle('sites/chess.com/styles.css');
|
||||
siteMatched = true;
|
||||
if (!siteMatched && (domain.includes('chess.com') || !!document.querySelector('.board-layout-main'))) {
|
||||
injectSiteStyle('sites/chess.com/styles.css'); siteMatched = true;
|
||||
}
|
||||
|
||||
const isAnilist = domain.includes('anilist.co');
|
||||
if (isAnilist) {
|
||||
injectSiteStyle('sites/anilist.co/styles.css');
|
||||
siteMatched = true;
|
||||
// 3. ANILIST
|
||||
if (!siteMatched && domain.includes('anilist.co')) {
|
||||
injectSiteStyle('sites/anilist.co/styles.css'); siteMatched = true;
|
||||
}
|
||||
|
||||
// 3. GITEA
|
||||
const isGitea = !!document.querySelector('meta[content*="gitea"]') ||
|
||||
!!document.querySelector('.ui.footer .item[href*="gitea.com"]') ||
|
||||
domain.includes('gitea');
|
||||
|
||||
if (isGitea) {
|
||||
console.log("Borland Theme: Gitea detected.");
|
||||
injectSiteStyle('sites/gitea.local/styles.css');
|
||||
siteMatched = true;
|
||||
// 4. GITEA
|
||||
if (!siteMatched && (!!document.querySelector('meta[content*="gitea"]') || domain.includes('gitea'))) {
|
||||
injectSiteStyle('sites/gitea.local/styles.css'); siteMatched = true;
|
||||
}
|
||||
|
||||
// --- GLOBAL FALLBACK ---
|
||||
// Only inject global styles if NO specific site was matched
|
||||
if (!siteMatched) {
|
||||
console.log("Borland Theme: No specific site match. Applying global styles.");
|
||||
injectSiteStyle('styles.css');
|
||||
// 5. GITHUB
|
||||
if (!siteMatched && domain.includes('github.com')) {
|
||||
injectSiteStyle('sites/github.com/styles.css'); siteMatched = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!siteMatched) {
|
||||
injectSiteStyle('styles.css');
|
||||
}
|
||||
});
|
||||
|
||||
function injectSiteStyle(path) {
|
||||
|
||||
@@ -5,8 +5,15 @@
|
||||
"description": "Applies the classic Borland blue and yellow theme to all websites.",
|
||||
"permissions": [
|
||||
"storage",
|
||||
"tabs"
|
||||
"tabs",
|
||||
"scripting"
|
||||
],
|
||||
"host_permissions": [
|
||||
"<all_urls>"
|
||||
],
|
||||
"action": {
|
||||
"default_popup": "popup.html"
|
||||
},
|
||||
"content_scripts": [
|
||||
{
|
||||
"matches": [
|
||||
@@ -22,38 +29,14 @@
|
||||
}
|
||||
],
|
||||
"web_accessible_resources": [
|
||||
{
|
||||
"resources": [
|
||||
"styles.css"
|
||||
],
|
||||
"matches": [
|
||||
"<all_urls>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"resources": [
|
||||
"styles.css",
|
||||
"sites/sonarr.local/styles.css"
|
||||
],
|
||||
"matches": [
|
||||
"<all_urls>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"resources": [
|
||||
"sites/github.com/styles.css"
|
||||
],
|
||||
"matches": [
|
||||
"https://github.com/*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"resources": [
|
||||
"styles.css",
|
||||
"sites/sonarr.local/styles.css",
|
||||
"sites/chess.com/styles.css",
|
||||
"sites/gitea.local/styles.css",
|
||||
"sites/anilist.co/styles.css"
|
||||
"sites/anilist.co/styles.css",
|
||||
"sites/github.com/styles.css"
|
||||
],
|
||||
"matches": [
|
||||
"<all_urls>"
|
||||
|
||||
@@ -4,27 +4,68 @@
|
||||
<head>
|
||||
<style>
|
||||
body {
|
||||
width: 180px;
|
||||
padding: 10px;
|
||||
font-family: sans-serif;
|
||||
width: 220px;
|
||||
padding: 12px;
|
||||
font-family: 'Courier New', Courier, monospace;
|
||||
background: #0000A4;
|
||||
color: #FFFF4E;
|
||||
margin: 0;
|
||||
border: 2px solid #FFF;
|
||||
}
|
||||
|
||||
h4 {
|
||||
margin: 0 0 12px 0;
|
||||
font-size: 14px;
|
||||
text-align: center;
|
||||
border-bottom: 1px solid #FFF;
|
||||
padding-bottom: 5px;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.button-row {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
button {
|
||||
width: 100%;
|
||||
flex: 1;
|
||||
cursor: pointer;
|
||||
background: #4F4F4F;
|
||||
background: #0000A4;
|
||||
color: #FFFF4E;
|
||||
border: 1px solid #FFF;
|
||||
padding: 5px;
|
||||
border: 1px solid #FFFF4E;
|
||||
padding: 8px 4px;
|
||||
font-size: 10px;
|
||||
font-weight: bold;
|
||||
text-transform: uppercase;
|
||||
box-shadow: 3px 3px 0px #000;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background: #FFFF4E;
|
||||
color: #0000A4;
|
||||
}
|
||||
|
||||
button:active {
|
||||
box-shadow: none;
|
||||
transform: translate(2px, 2px);
|
||||
}
|
||||
|
||||
#theme-mode-btn {
|
||||
display: none;
|
||||
/* Managed by JS */
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h4 id="site-name">Domain</h4>
|
||||
<button id="toggle-btn">Disable on this site</button>
|
||||
<div class="button-row">
|
||||
<button id="toggle-btn">Disable</button>
|
||||
<button id="theme-mode-btn">Static Theme</button>
|
||||
</div>
|
||||
<script src="popup.js"></script>
|
||||
</body>
|
||||
|
||||
|
||||
@@ -1,22 +1,59 @@
|
||||
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
||||
const url = new URL(tabs[0].url);
|
||||
chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => {
|
||||
if (!tabs[0] || !tabs[0].url) return;
|
||||
|
||||
const activeTab = tabs[0];
|
||||
const url = new URL(activeTab.url);
|
||||
const domain = url.hostname;
|
||||
document.getElementById('site-name').textContent = domain;
|
||||
|
||||
const btn = document.getElementById('toggle-btn');
|
||||
const themeBtn = document.getElementById('theme-mode-btn');
|
||||
|
||||
chrome.storage.local.get([domain], (result) => {
|
||||
let hasSpecificTheme = false;
|
||||
|
||||
try {
|
||||
// Run detection logic inside the tab to match content.js logic
|
||||
const detectionResult = await chrome.scripting.executeScript({
|
||||
target: { tabId: activeTab.id },
|
||||
func: () => {
|
||||
const domain = window.location.hostname;
|
||||
const isSonarr = getComputedStyle(document.documentElement).getPropertyValue('--sonarrBlue').trim() !== "" ||
|
||||
document.title.toLowerCase().includes('sonarr');
|
||||
const isChess = domain.includes('chess.com') || !!document.querySelector('.board-layout-main');
|
||||
const isAnilist = domain.includes('anilist.co');
|
||||
const isGitea = !!document.querySelector('meta[content*="gitea"]') || domain.includes('gitea');
|
||||
const isGithub = domain.includes('github.com');
|
||||
|
||||
return isSonarr || isChess || isAnilist || isGitea || isGithub;
|
||||
}
|
||||
});
|
||||
hasSpecificTheme = detectionResult[0]?.result || false;
|
||||
} catch (e) {
|
||||
console.warn("Could not inspect page for site-specific theme:", e);
|
||||
}
|
||||
|
||||
chrome.storage.local.get([domain, `${domain}_mode`], (result) => {
|
||||
const isDisabled = result[domain] === 'disabled';
|
||||
btn.textContent = isDisabled ? "Enable on this site" : "Disable on this site";
|
||||
btn.textContent = isDisabled ? "Enable" : "Disable";
|
||||
|
||||
btn.onclick = () => {
|
||||
if (isDisabled) {
|
||||
chrome.storage.local.remove(domain);
|
||||
} else {
|
||||
chrome.storage.local.set({ [domain]: 'disabled' });
|
||||
}
|
||||
chrome.tabs.reload(tabs[0].id);
|
||||
if (isDisabled) chrome.storage.local.remove(domain);
|
||||
else chrome.storage.local.set({ [domain]: 'disabled' });
|
||||
chrome.tabs.reload(activeTab.id);
|
||||
window.close();
|
||||
};
|
||||
|
||||
if (hasSpecificTheme && !isDisabled) {
|
||||
themeBtn.style.display = 'block';
|
||||
const isGlobalMode = result[`${domain}_mode`] === 'global';
|
||||
themeBtn.textContent = isGlobalMode ? "Site Theme" : "Static Theme";
|
||||
|
||||
themeBtn.onclick = () => {
|
||||
const newMode = isGlobalMode ? 'site' : 'global';
|
||||
chrome.storage.local.set({ [`${domain}_mode`]: newMode });
|
||||
chrome.tabs.reload(activeTab.id);
|
||||
window.close();
|
||||
};
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user