Skip to content

Commit

Permalink
Add compatibility with Firefox. Fix bugs. Improve performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
turanjanin committed Sep 6, 2024
1 parent 4a849a4 commit 8692cd1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
_metadata/
build/
.idea/
*.swp
.DS_Store
manifest.json
32 changes: 20 additions & 12 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ chrome.runtime.onInstalled.addListener(function (details) {
return;
}

const initialEnabledRedirects = [];

const initialEnabledRedirects = Array.from(Object.keys(patternsToRules));
enabledRedirects = initialEnabledRedirects;
chrome.storage.local.set({ enabledRedirects: initialEnabledRedirects });
console.log('Initial redirect list stored');
Expand All @@ -92,6 +91,7 @@ chrome.runtime.onInstalled.addListener(function (details) {
'besplatniprogrami.org',
'beta.rs',
'bif.rs',
'biznis.kurir.rs',
'brainz.center',
'bujanovacke.co.rs',
'buro247.rs',
Expand Down Expand Up @@ -334,10 +334,10 @@ function transliteratePage(tab) {
allFrames: true
},
files: ['/content.js']
}).then(() => {
updateExtensionIcon(tab, true);
chrome.tabs.sendMessage(tab.id, { isEnabled: true });
});

updateExtensionIcon(tab, true);
chrome.tabs.sendMessage(tab.id, { isEnabled: true });
}

function matchPattern(url, pattern) {
Expand All @@ -356,7 +356,15 @@ function getMatchingPattern(url) {
}

function redirectPage(tab) {
updateExtensionIcon(tab, true);
// Trigger page reload in order for declarativeNetRequest rules to perform the actual redirection.
chrome.scripting.executeScript({
target: {
tabId: tab.id
},
files: ['/reload.js']
}).then(() => {
updateExtensionIcon(tab, true);
});
}

function showOriginalPage(tab) {
Expand All @@ -382,7 +390,7 @@ function updateActiveTab() {

const matchingPattern = getMatchingPattern(activeTab.url);
if (enabledRedirects.includes(matchingPattern)) {
redirectPage(activeTab);
updateExtensionIcon(activeTab, true);
return;
}

Expand Down Expand Up @@ -427,9 +435,9 @@ chrome.storage.local.get({enabledDomains: []}, function (result) {

chrome.action.onClicked.addListener(toggleDomain);

chrome.tabs.onUpdated.addListener(updateActiveTab);
chrome.tabs.onActivated.addListener(updateActiveTab);
chrome.windows.onFocusChanged.addListener(updateActiveTab);


updateActiveTab();
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status && changeInfo.status === 'loading') {
updateActiveTab();
}
});
41 changes: 41 additions & 0 deletions manifest-chrome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"manifest_version": 3,
"name": "Ћирилизатор",
"description": "Претвара латиничне стране у ћириличне.",
"author": "Јован Турањанин",
"version": "3.0",
"background": {
"service_worker": "background.js"
},
"action": {
"default_icon": {
"16": "icons/icon-off-16.png",
"32": "icons/icon-off-32.png",
"48": "icons/icon-off-48.png",
"128": "icons/icon-off-128.png"
},
"default_title": "Ћирилизуј"
},
"icons": {
"16": "icons/icon-on-16.png",
"32": "icons/icon-on-32.png",
"48": "icons/icon-on-48.png",
"128": "icons/icon-on-128.png"
},
"declarative_net_request": {
"rule_resources" : [{
"id": "map",
"enabled": true,
"path": "latin-cyrillic-domains.json"
}]
},
"permissions": [
"declarativeNetRequestWithHostAccess",
"scripting",
"storage",
"tabs"
],
"host_permissions": [
"<all_urls>"
]
}
2 changes: 1 addition & 1 deletion manifest.json → manifest-firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Јован Турањанин",
"version": "3.0",
"background": {
"service_worker": "background.js"
"scripts": ["background.js"]
},
"action": {
"default_icon": {
Expand Down
2 changes: 2 additions & 0 deletions reload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
window.location.reload();

0 comments on commit 8692cd1

Please sign in to comment.