Skip to content

Commit

Permalink
[WIP] Updating Manifest V3 (#9)
Browse files Browse the repository at this point in the history
* Start of migration towards Manifest V3
* Added a rules.json for Manifest V3 (for blocking)
* Manifest V2 support (firefox)
* Moved the manifest_v3 now to his own file
* Gitignore generated_indexed_rulesets, manifest.json
* Added a build without signing for testing to GH actions
* Fixed manifest V2 compatibility & Firefox issues
  • Loading branch information
OhMyGuus authored Sep 25, 2022
1 parent 1e843b4 commit 981140f
Show file tree
Hide file tree
Showing 7 changed files with 25,040 additions and 46 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Build extension

on:
push:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Move to manifest v3
run: |
cp src/manifest_v3.json src/manifest.json
- name: Archive Release
run: |
7z a 'IDCAC-chrome.zip' -r './src/*'
- uses: actions/upload-artifact@v3
with:
name: IDCAC-chrome
path: 'IDCAC-chrome.zip'

- name: Move to manifest v2
run: |
rm src/manifest.json
cp src/manifest_v2.json src/manifest.json
- name: "web-ext build"
id: web-ext-build
uses: kewisch/action-web-ext@v1
with:
cmd: build
source: src
filename: "IDCAC.xpi"

- uses: actions/upload-artifact@v3
with:
name: IDCAC-firefox-unsigned
path: ${{ steps.web-ext-build.outputs.target }}






11 changes: 10 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Release extention
name: Release extension

on:
workflow_dispatch:
Expand All @@ -14,6 +14,10 @@ jobs:
run: |
echo "::set-output name=release_tag::IDAC_$(date +"%Y.%m.%d_%H-%M")"
- name: Move to manifest v3
run: |
cp src/manifest_v3.json src/manifest.json
- name: Archive Release
run: |
7z a 'IDCAC-chrome.zip' -r './src/*'
Expand All @@ -22,6 +26,11 @@ jobs:
with:
name: IDCAC-chrome
path: 'IDCAC-chrome.zip'

- name: Move to manifest v2
run: |
rm src/manifest.json
cp src/manifest_v2.json src/manifest.json
- name: "web-ext build"
id: web-ext-build
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/src/manifest.json
/src/_metadata/generated_indexed_rulesets
170 changes: 126 additions & 44 deletions src/data/context-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@

var cached_rules = {},
whitelisted_domains = {},
tab_list = {};

tab_list = {},
xml_tabs = {};
lastDeclarativeNetRuleId = 1;

const isManifestV3 = chrome.runtime.getManifest().manifest_version == 3;

if (isManifestV3) {
/* rules.js */
try {
importScripts("rules.js");
} catch (e) {
console.log(e);
}
}

// Common functions

Expand All @@ -26,18 +38,47 @@ function getHostname(url, cleanup)


// Whitelisting
function updateWhitelist() {
lastDeclarativeNetRuleId = 1;
chrome.storage.local.get('whitelisted_domains', async (storedWhitelist) => {

function updateWhitelist()
{
chrome.storage.local.get('whitelisted_domains', function(r) {
if (typeof r.whitelisted_domains != 'undefined')
whitelisted_domains = r.whitelisted_domains;
if (typeof storedWhitelist.whitelisted_domains != 'undefined')
whitelisted_domains = storedWhitelist.whitelisted_domains;

if (isManifestV3) {
await UpdateWhitelistRules();
}
});
}

async function UpdateWhitelistRules() {
if (!isManifestV3) {
console.warn("Called unsupported function")
return;
}
let previousRules = (await chrome.declarativeNetRequest.getDynamicRules()).map((v) => { return v.id; });
let addRules = Object.entries(whitelisted_domains).filter((element) => element[1]).map((v) => {
return {
"id": lastDeclarativeNetRuleId++,
"priority": 1,
"action": { "type": "allow" },
"condition": {
"urlFilter": "*", "resourceTypes": ["script", "stylesheet", "xmlhttprequest", "image"],
"initiatorDomains": [v[0]]
}
}
});

chrome.declarativeNetRequest.updateDynamicRules(
{
addRules,
removeRuleIds: previousRules
});
}

updateWhitelist();

chrome.runtime.onMessage.addListener(function(request, info){
chrome.runtime.onMessage.addListener(async function(request, info){
if (request == 'update_whitelist')
updateWhitelist();
});
Expand Down Expand Up @@ -66,24 +107,25 @@ function getWhitelistedDomain(tab)
return false;
}

function toggleWhitelist(tab)
{
async function toggleWhitelist(tab) {
if (tab.url.indexOf('http') != 0 || !tab_list[tab.id])
return;

if (tab_list[tab.id].whitelisted)
{

if (tab_list[tab.id].whitelisted) {
var hostname = getWhitelistedDomain(tab_list[tab.id]);
delete whitelisted_domains[tab_list[tab.id].hostname];
}
else
whitelisted_domains[tab_list[tab.id].hostname] = true;
chrome.storage.local.set({'whitelisted_domains': whitelisted_domains}, function(){

chrome.storage.local.set({ 'whitelisted_domains': whitelisted_domains }, function () {
for (var i in tab_list)
if (tab_list[i].hostname == tab_list[tab.id].hostname)
tab_list[i].whitelisted = !tab_list[tab.id].whitelisted;
});
if (isManifestV3) {
await UpdateWhitelistRules();
}
}


Expand Down Expand Up @@ -162,7 +204,8 @@ chrome.runtime.onInstalled.addListener(function(d){
function blockUrlCallback(d)
{
// Cached request: find the appropriate tab

//TODO: parse rules.json for this function.

if (d.tabId == -1 && d.initiator) {
let hostname = getHostname(d.initiator, true);

Expand Down Expand Up @@ -248,10 +291,23 @@ function blockUrlCallback(d)

return {cancel:false};
}

chrome.webRequest.onBeforeRequest.addListener(blockUrlCallback, {urls:["http://*/*", "https://*/*"], types:["script","stylesheet","xmlhttprequest"]}, ["blocking"]);
if (!isManifestV3) {
chrome.webRequest.onBeforeRequest.addListener(blockUrlCallback, { urls: ["http://*/*", "https://*/*"], types: ["script", "stylesheet", "xmlhttprequest"] }, ["blocking"]);


chrome.webRequest.onHeadersReceived.addListener(function(d) {
if (tab_list[d.tabId]) {
d.responseHeaders.forEach(function(h) {
if (h.name == "Content-Type" || h.name == "content-type") {
xml_tabs[d.tabId] = h.value.indexOf("/xml") > -1;
}
});
}

return {cancel:false};
}, {urls:["http://*/*", "https://*/*"], types:["main_frame"]}, ["blocking", "responseHeaders"]);

}
// Reporting

function reportWebsite(info, tab)
Expand Down Expand Up @@ -291,23 +347,27 @@ function activateDomain(hostname, tabId, frameId)
if (!cached_rules[hostname])
return false;

let r = cached_rules[hostname],
let cached_rule = cached_rules[hostname],
status = false;

if (typeof r.s != 'undefined') {
chrome.tabs.insertCSS(tabId, {code: r.s, frameId: frameId, matchAboutBlank: true, runAt: 'document_start'});
// cached_rule.s = Custom css for webpage
// cached_rule.c = Common css for webpage
// cached_rule.j = Common js for webpage

if (typeof cached_rule.s != 'undefined') {
insertCSS({ tabId, frameId: frameId || 0, css: cached_rule.s });
status = true;
}
else if (typeof r.c != 'undefined') {
chrome.tabs.insertCSS(tabId, {code: commons[r.c], frameId: frameId, matchAboutBlank: true, runAt: 'document_start'});
else if (typeof cached_rule.c != 'undefined') {
insertCSS({ tabId, frameId: frameId || 0, css: commons[cached_rule.c] });
status = true;
}
if (typeof r.j != 'undefined') {
chrome.tabs.executeScript(tabId, {file: 'data/js/'+(r.j > 0 ? 'common'+r.j : hostname)+'.js', frameId: frameId, matchAboutBlank: true, runAt: 'document_end'});

if (typeof cached_rule.j != 'undefined') {
executeScript({ tabId, frameId, file: 'data/js/' + (cached_rule.j > 0 ? 'common' + cached_rule.j : hostname) + '.js' });
status = true;
}

return status;
}

Expand All @@ -319,34 +379,33 @@ function doTheMagic(tabId, frameId, anotherTry)

if (tab_list[tabId].whitelisted)
return;

// Common CSS rules
chrome.tabs.insertCSS(tabId, {file: "data/css/common.css", frameId: frameId || 0, matchAboutBlank: true, runAt: 'document_start'}, function() {

insertCSS({ tabId, frameId: frameId || 0, file: "data/css/common.css" }, function () {
// A failure? Retry.

if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError);

let currentTry = (anotherTry || 1);

if (currentTry == 5)
return;

return doTheMagic(tabId, frameId || 0, currentTry + 1);
}



// Common social embeds
chrome.tabs.executeScript(tabId, {file:'data/js/embeds.js', frameId: frameId || 0, matchAboutBlank: true, runAt: 'document_end'}, function() {});
executeScript({ tabId, frameId, file: 'data/js/embeds.js' });

if (activateDomain(tab_list[tabId].hostname, tabId, frameId || 0))
return;

for (var level in tab_list[tabId].host_levels)
if (activateDomain(tab_list[tabId].host_levels[level], tabId, frameId || 0))
return true;

// Common JS rules when custom rules don't exist
chrome.tabs.executeScript(tabId, {file:'data/js/common.js', frameId: frameId || 0, matchAboutBlank: true, runAt: 'document_end'}, function() {});
executeScript({ tabId, frameId, file: 'data/js/common.js' });
});
}

Expand Down Expand Up @@ -382,20 +441,43 @@ chrome.runtime.onMessage.addListener(function(request, info, sendResponse) {

if (response.tab.whitelisted)
response.tab.hostname = getWhitelistedDomain(tab_list[request.tabId]);

sendResponse(response);
}
else if (request.command == 'toggle_extension')
toggleWhitelist(tab_list[request.tabId]);
toggleWhitelist(tab_list[request.tabId]);
else if (request.command == 'report_website')
chrome.tabs.create({url:"https://github.com/OhMyGuus/I-Dont-Care-About-Cookies/issues/new"});
else if (request.command == 'refresh_page')
chrome.tabs.executeScript(request.tabId, {code:'window.location.reload();'});
executeScript({ tabId: request.tabId, func: () => { window.location.reload(); } });
}
else
{
if (request.command == 'open_options_page')
chrome.tabs.create({url:chrome.runtime.getURL('data/options.html')});
}
}
});
});



function insertCSS(injection, callback) {
let { tabId, css, file, frameId } = injection

if (isManifestV3) {
chrome.scripting.insertCSS({ target: { tabId: tabId, frameIds: [frameId || 0] }, css: css, files: file ? [file] : undefined }, callback);
} else {
chrome.tabs.insertCSS(tabId, { file, code: css, frameId: frameId || 0, runAt: xml_tabs[tabId] ? 'document_idle' : 'document_start' }, callback)
}
}

function executeScript(injection, callback) {
let { tabId, func, file, frameId } = injection
if (isManifestV3) {
// manifest v3
chrome.scripting.executeScript({ target: { tabId, frameIds: [frameId || 0] }, files: file ? [file] : undefined, func }, callback);
} else {
// manifest v2
chrome.tabs.executeScript(tabId, { file, frameId: frameId || 0, code: func == undefined ? undefined : "(" + func.toString() + ")();", runAt: xml_tabs[tabId] ? 'document_idle' : 'document_end' }, callback);
}
}
1 change: 0 additions & 1 deletion src/manifest.json → src/manifest_v2.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"update_url": "https://clients2.google.com/service/update2/crx",
"manifest_version": 2,
"name": "__MSG_extensionName__",
"short_name": "__MSG_extensionDescription__",
Expand Down
Loading

0 comments on commit 981140f

Please sign in to comment.