Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actuallyaridan committed Nov 22, 2023
2 parents f073ea8 + 1adda9e commit 3706721
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,4 @@ Simplified Chinese - [am1006](https://github.com/am1006), [CarimoWuling](https:/
Czech - Menal
German - [basti564](https://twitter.com/basti564)
Catalan - [elmees21](https://twitter.com/elmees21)
Swedish - [actuallyaridan](https://twitter.com/actuallyaridan)
1 change: 1 addition & 0 deletions layouts/settings/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ <h1>__MSG_oldtwitter_settings__</h1><br>
<option value="pl">Polski (Polish)</option>
<option value="uk">Українська (Ukrainian)</option>
<option value="it">Italiano (Italian)</option>
<option value="sv">Svenska (Swedish)</option>
<option value="ro">Română (Romanian)</option>
<option value="lv">Latviešu (Latvian)</option>
<option value="th">ภาษาไทย (Thai)</option>
Expand Down
58 changes: 50 additions & 8 deletions layouts/settings/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,44 @@ function renderUserData() {
});
}

async function writeCSSToDB(cssData) {
let db = await openDatabase();
return new Promise((resolve, reject) => {
let transaction = db.transaction(["cssStore"], "readwrite");
let store = transaction.objectStore("cssStore");
let request = store.put({ id: "customCSS", css: cssData });

request.onerror = function(event) {
reject("Error writing CSS to DB");
};

request.onsuccess = function(event) {
resolve();
};
});
}

async function readCSSFromDB() {
let db = await openDatabase();
return new Promise((resolve, reject) => {
let transaction = db.transaction(["cssStore"]);
let objectStore = transaction.objectStore("cssStore");
let request = objectStore.get("customCSS");

request.onerror = function(event) {
reject("Error reading CSS");
};

request.onsuccess = function(event) {
if (request.result) {
resolve(request.result.css);
} else {
resolve('');
}
};
});
}

setTimeout(async () => {
if(!vars) {
await loadVars();
Expand Down Expand Up @@ -853,12 +891,14 @@ setTimeout(async () => {
}
});
customCSSSave.addEventListener('click', () => {
chrome.storage.sync.set({
customCSS: customCSS.value
}, () => {
let event = new CustomEvent('customCSS', { detail: customCSS.value });
customCSSBus.postMessage({type: 'css'});
let cssValue = customCSS.value;

writeCSSToDB(cssValue).then(() => {
let event = new CustomEvent('customCSS', { detail: cssValue });
customCSSBus.postMessage({ type: 'css' });
document.dispatchEvent(event);
}).catch(error => {
console.error("Error saving CSS to DB:", error);
});
});
autotranslateLanguageList.addEventListener('change', () => {
Expand Down Expand Up @@ -996,8 +1036,9 @@ setTimeout(async () => {
showQuoteCount.checked = !!vars.showQuoteCount;
hideUnfollowersPage.checked = !!vars.hideUnfollowersPage;
if(vars.customCSS) {
customCSS.value = vars.customCSS;
writeCSSToDB(vars.customCSS)
}
customCSS.value = await readCSSFromDB();
document.getElementById('stt-div').hidden = vars.timelineType !== 'algo' && vars.timelineType !== 'algov2';
savePreferredQuality.checked = !!vars.savePreferredQuality;
showOriginalImages.checked = !!vars.showOriginalImages;
Expand Down Expand Up @@ -1123,10 +1164,11 @@ setTimeout(async () => {
});
input.click();
});
document.getElementById('export-style').addEventListener('click', () => {
document.getElementById('export-style').addEventListener('click', async () => {
let customCssfromDb = await readCSSFromDB();
let json = {
customCSSVariables: vars.customCSSVariables,
customCSS: vars.customCSS,
customCSS: customCssfromDb,
font: vars.font,
tweetFont: vars.tweetFont,
linkColor: vars.linkColor
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Old Twitter Layout (2023)",
"description": "__MSG_ext_description__",
"version": "1.8.1.4",
"version": "1.8.1.5",
"manifest_version": 3,
"homepage_url": "https://github.com/dimdenGD/OldTwitter",
"background": {
Expand Down
81 changes: 76 additions & 5 deletions scripts/injection.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ if (
url.searchParams.set('newtwitter', 'true');
location.replace(url);
}
const LANGUAGES = ["en", "ru", "uk", "fr", "pt_BR", "es", "el", "ro", "tl", "lv", "he", "ne", "nl", "ja", "tr", "it", "ar", "th", "ko", "pl", "vi", "zh_CN", "zh_TW", "cs", "de", "ca"];
const LANGUAGES = ["en", "ru", "uk", "fr", "pt_BR", "es", "el", "ro", "tl", "lv", "he", "ne", "nl", "ja", "tr", "it", "ar", "th", "ko", "pl", "vi", "zh_CN", "zh_TW", "cs", "de", "ca", "sv"];
const TRANSLATORS = {
"ru": ["dimden", "https://dimden.dev/"],
"uk": ["dimden", "https://dimden.dev/"],
Expand Down Expand Up @@ -205,7 +205,8 @@ const TRANSLATORS = {
],
"cs": ["Menal"],
"de": ["basti564", "https://twitter.com/basti564"],
"ca": ["elmees21", "https://twitter.com/elmees21"]
"ca": ["elmees21", "https://twitter.com/elmees21"],
"sv": ["actuallyaridan", "https://twitter.com/actuallyaridan"]
};
let LOC = {};
let LOC_EN = {};
Expand All @@ -226,20 +227,89 @@ function isDark() {
return hours < 9 || hours >= 19;
}
let customCSS, profileCSS = false;

async function openDatabase() {
return new Promise((resolve, reject) => {
let request = indexedDB.open("CustomCSSDatabase", 1);

request.onerror = function(event) {
reject('Database error: ' + event.target.errorCode);
};

request.onsuccess = function(event) {
resolve(event.target.result);
};

// Only runs if the database was just created (like on first run)
request.onupgradeneeded = function(event) {
let db = event.target.result;
db.createObjectStore("cssStore", { keyPath: "id" });
};
});
}

async function readCSSFromDB() {
let db = await openDatabase();
return new Promise((resolve, reject) => {
let transaction = db.transaction(["cssStore"]);
let objectStore = transaction.objectStore("cssStore");
let request = objectStore.get("customCSS");

request.onerror = function(event) {
reject("Error reading CSS");
};

request.onsuccess = function(event) {
if (request.result) {
resolve(request.result.css);
} else {
resolve('');
}
};
});
}

async function writeCSSToDB(cssData) {
let db = await openDatabase();
return new Promise((resolve, reject) => {
let transaction = db.transaction(["cssStore"], "readwrite");
let store = transaction.objectStore("cssStore");
let request = store.put({ id: "customCSS", css: cssData });

request.onerror = function(event) {
reject("Error writing CSS to DB");
};

request.onsuccess = function(event) {
resolve();
};
});
}

async function updateCustomCSS() {
let data = await new Promise(resolve => {
chrome.storage.sync.get(['customCSS'], data => {
resolve(data);
});
});
if(!data.customCSS) data.customCSS = '';

if(data.customCSS) {
writeCSSToDB(data.customCSS)
chrome.storage.sync.remove('customCSS');
}
data.customCSS = await readCSSFromDB();

if(profileCSS) return;

if(customCSS) customCSS.remove();

customCSS = document.createElement('style');
customCSS.id = 'oldtwitter-custom-css';
customCSS.innerHTML = data.customCSS;
if(document.head) document.head.appendChild(customCSS);
else {

if(document.head) {
document.head.appendChild(customCSS);
} else {
let int = setInterval(() => {
if(document.head) {
clearInterval(int);
Expand All @@ -248,6 +318,7 @@ async function updateCustomCSS() {
}, 100);
}
}

async function updateCustomCSSVariables() {
let root = document.querySelector(":root");
let data = await new Promise(resolve => {
Expand Down

0 comments on commit 3706721

Please sign in to comment.