Skip to content

Commit

Permalink
Made compatible for manifestv2
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythack committed Dec 13, 2023
1 parent f64a9ac commit c82c661
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 19 deletions.
66 changes: 48 additions & 18 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
if (typeof browser === "undefined") {
var browser = chrome;
var browser_action = browser.action
function insertCSS(tabId, css) {
browser.scripting.insertCSS({
target: {
tabId: tabId,
},
css: css
}).then(result => {return;}, error => {console.log(error);});
};
function removeCSS(tabId, css) {
browser.scripting.removeCSS({
target: {
tabId: tabId,
},
css: css
}).catch(error => {return;});
};
} else {
var browser_action = browser.browserAction
function insertCSS(tabId, css) {
browser.tabs.insertCSS(tabId, {
code: css,
}).then(
() => {
return;
},
(error) => {
console.error(error);
}
);
};
function removeCSS(tabId, css) {
browser.tabs.removeCSS(tabId, {
code: css,
}).catch((error) => {
console.error(error);
});
};
}

var localstorage = new Object(); // Initialize a local storage
Expand All @@ -16,12 +54,7 @@ function matchRuleShort(str, rule) {
async function restoreOptions(tab) {
var storage = await browser.storage.local.get(); // Get settings
if (localstorage[tab.id]) { // If extension already injected CSS in this tab, remove it
browser.scripting.removeCSS({
target: {
tabId: tab.id,
},
css: localstorage[tab.id]
}).catch(error => {return;});
removeCSS(tab.id, localstorage[tab.id])
}
var css;
var url = new URL(tab.url);
Expand All @@ -35,23 +68,20 @@ async function restoreOptions(tab) {
}
});
}
if (!injected && storage.witness) { // If url didn't match any custom settings and the user already defined some settings (avoid injecting undefined values into CSS)
if (!injected && storage.witness && !/^((chrome:\/\/|chrome-extension:\/\/|about:).*|$|https:\/\/chrome\.google\.com\/webstore.*|https:\/\/addons\.mozilla\.org.*)/.test(tab.url)) { // If url didn't match any custom settings and the user already defined some settings (avoid injecting undefined values into CSS)
css = '::selection { background: ' + storage.background_color + ' !important; color: ' + storage.color + ' !important;' + ((storage.shadowActivated) ? 'text-shadow: ' + storage.shadowColor + ' 0px 0px ' + storage.shadowBlur + 'px !important;' : '') + ((storage.decorationActivated) ? 'text-decoration: ' + storage.decorationType + ' ' + storage.decorationColor + ' !important;' : '') + '}';
}
browser.scripting.insertCSS({
target: {
tabId: tab.id,
},
css: css
}).then(result => {return;}, error => {console.log(error);});
if (css) {
insertCSS(tab.id, css);
}
localstorage[tab.id] = css; // Store the injected CSS into local storage so that we can remove it later
}

async function update_action_icon(tabin) {
var tab = await browser.tabs.get(tabin.tabId); // Get the tab from the id
var storage = await browser.storage.local.get(); // Get settings
if (/^((chrome:\/\/|chrome-extension:\/\/|about:).*|$|https:\/\/chrome\.google\.com\/webstore.*|https:\/\/addons\.mozilla\.org.*)/.test(tab.url)) { // If tab is on chrome://, about:// or on the chrome web store
browser.action.setIcon({path: './images/icondisabled.png'}); // Set the icon to disabled (grey)
browser_action.setIcon({path: './images/icondisabled.png'}); // Set the icon to disabled (grey)
return; // Abort
}
try {
Expand All @@ -64,12 +94,12 @@ async function update_action_icon(tabin) {
storage.customOptions.forEach((element) => { // For each custom setting
if (matchRuleShort(taburl.host, element.url)) { // If the custom setting's url matches the hostname
injected = true;
browser.action.setIcon({path: './images/iconcustom.png'}); // Set to custom icon (yellow)
browser_action.setIcon({path: './images/iconcustom.png'}); // Set to custom icon (yellow)
}
});
}
if (!injected) { // If the default settings are applied
browser.action.setIcon({path: './images/icon.png'}); // Set to the default icon (blue)
browser_action.setIcon({path: './images/icon.png'}); // Set to the default icon (blue)
}
}

Expand Down Expand Up @@ -104,7 +134,7 @@ browser.runtime.onMessage.addListener((message, sender) => {

browser.tabs.onActivated.addListener(update_action_icon); // When the active tab has changed: update icon

chrome.runtime.onInstalled.addListener(async details => {
browser.runtime.onInstalled.addListener(async details => {
switch (details.reason) {
case "install":
browser.storage.local.set({ // Set basic settings
Expand Down Expand Up @@ -153,4 +183,4 @@ chrome.runtime.onInstalled.addListener(async details => {
}
});

chrome.runtime.setUninstallURL("https://forms.gle/uRLUAXrwUa7bbBRH8");
browser.runtime.setUninstallURL("https://forms.gle/uRLUAXrwUa7bbBRH8");
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "__MSG_appName__",
"author": "Pythack",
"default_locale": "en",
"version": "2.0.1",
"version": "2.1.0",
"description": "__MSG_appDesc__",
"background": {
"service_worker": "./background.js"
Expand Down
40 changes: 40 additions & 0 deletions manifestv2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"manifest_version": 2,
"name": "__MSG_appName__",
"author": "Pythack",
"default_locale": "en",
"version": "2.1.1",
"description": "__MSG_appDesc__",
"background": {
"scripts": ["background.js"],
"persistent": false
},
"permissions": [
"<all_urls>",
"storage",
"tabs",
"activeTab"
],
"browser_action": {
"default_icon": {
"16": "images/icon-16.png",
"48": "images/icon-48.png",
"128": "images/icon.png"
},
"default_title": "__MSG_appName__",
"default_popup": "popup.html"
},
"icons": {
"16": "images/icon-16.png",
"48": "images/icon-48.png",
"128": "images/icon.png"
},
"web_accessible_resources": [
"images/icon.png",
"images/iconcustom.png",
"images/iconvalid.png",
"images/iconfail.png",
"images/icondisabled.png"
]
}

2 changes: 2 additions & 0 deletions popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ body {
height: fit-content;
margin: 0;
width: 100%;
width: 300px;
}

#formdiv {
Expand Down Expand Up @@ -240,6 +241,7 @@ input[type="url"]:placeholder-shown:focus {
position: fixed;
padding-top: 5px;
bottom: 0;
left: 0;
background-color: #f3f3f3;
box-shadow: 0 0 15px rgb(200, 200, 200);
}
Expand Down
6 changes: 6 additions & 0 deletions popup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
if (typeof browser === "undefined") {
var browser = chrome;
text_decoration = true;
} else {
text_decoration = false;
}

function saveSuccess(item) {
Expand Down Expand Up @@ -225,6 +228,9 @@ function restoreOptions() {
select.dispatchEvent(new Event('change'));
}
});
if (!text_decoration) {
document.querySelector("#formdivtxtdec").style.display = "none";
};
};


Expand Down

0 comments on commit c82c661

Please sign in to comment.