Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Commit

Permalink
Case-insensitive comparsions and promote the main_frame rulesets
Browse files Browse the repository at this point in the history
  • Loading branch information
Pasu Chan Chak Shing committed Feb 19, 2018
1 parent 55c7af4 commit f254015
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions chromium/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ chrome.browserAction.onClicked.addListener(e => {
*/
function AppliedRulesets() {
this.active_tab_rules = new Map();
this.active_tab_main_frames = new Map();

let that = this;
if (chrome.tabs) {
Expand All @@ -176,24 +177,38 @@ function AppliedRulesets() {
}

AppliedRulesets.prototype = {
addRulesetToTab: function(tabId, ruleset) {
addRulesetToTab: function(tabId, type, ruleset) {
if (!this.active_tab_main_frames.has(tabId)) {
this.active_tab_main_frames.set(tabId, false);
}

// always show main_frame ruleset on the top
if (type == "main_frame") {
this.active_tab_main_frames.set(tabId, true);
this.active_tab_rules.set(tabId, [ruleset,]);
return ;
}

if (this.active_tab_rules.has(tabId)) {
let rulesets = this.active_tab_rules.get(tabId);
let insertIndex = 0;

const ruleset_name = ruleset.name.toLowerCase();

for (const item of rulesets) {
if (item.name == ruleset.name) {
const item_name = item.name.toLowerCase();

if (item_name == ruleset_name) {
return ;
} else if (item.name < ruleset.name) {
} else if (insertIndex == 0 && this.active_tab_main_frames.get(tabId)) {
insertIndex = 1;
} else if (item_name < ruleset_name) {
insertIndex++;
} else {
break;
}
}
rulesets.splice(insertIndex, 0, ruleset);
} else {
this.active_tab_rules.set(tabId, []);
this.addRulesetToTab(tabId, ruleset);
this.active_tab_rules.set(tabId, [ruleset,]);
}
},

Expand All @@ -207,6 +222,7 @@ AppliedRulesets.prototype = {

removeTab: function(tabId) {
this.active_tab_rules.delete(tabId);
this.active_tab_main_frames.delete(tabId);
},

getActiveRulesetCount: function (tabId) {
Expand Down Expand Up @@ -300,7 +316,7 @@ function onBeforeRequest(details) {
var newuristr = null;

for (let ruleset of potentiallyApplicable) {
appliedRulesets.addRulesetToTab(details.tabId, ruleset);
appliedRulesets.addRulesetToTab(details.tabId, details.type, ruleset);
if (ruleset.active && !newuristr) {
newuristr = ruleset.apply(canonical_url);
}
Expand Down

0 comments on commit f254015

Please sign in to comment.