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

Order rulesets by names in popup, Fix #11986 #14678

Merged
merged 4 commits into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 74 additions & 42 deletions chromium/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,6 @@ var switchPlannerEnabledFor = {};
// rw / nrw stand for "rewritten" versus "not rewritten"
var switchPlannerInfo = {};

function getActiveRulesetCount(id) {
const applied = activeRulesets.getRulesets(id);

if (!applied)
{
return 0;
}

let activeCount = 0;

for (const key in applied) {
if (applied[key].active) {
activeCount++;
}
}

return activeCount;
}

/**
* Set the icon color correctly
* active: extension is enabled.
Expand Down Expand Up @@ -154,7 +135,7 @@ function updateState () {
return;
}
const tabId = tabs[0].id;
const activeCount = getActiveRulesetCount(tabId);
const activeCount = appliedRulesets.getActiveRulesetCount(tabId);

if ('setBadgeBackgroundColor' in chrome.browserAction) {
chrome.browserAction.setBadgeBackgroundColor({ color: '#666666', tabId });
Expand All @@ -178,13 +159,16 @@ chrome.browserAction.onClicked.addListener(e => {
});
});



/**
* Adds a listener for removed tabs
* */
* Add a listener for removed tabs
*/
function AppliedRulesets() {
this.active_tab_rules = {};
this.active_tab_rules = new Map();
this.active_tab_main_frames = new Map();

var that = this;
let that = this;
if (chrome.tabs) {
chrome.tabs.onRemoved.addListener(function(tabId) {
that.removeTab(tabId);
Expand All @@ -193,29 +177,70 @@ function AppliedRulesets() {
}

AppliedRulesets.prototype = {
addRulesetToTab: function(tabId, ruleset) {
if (tabId in this.active_tab_rules) {
this.active_tab_rules[tabId][ruleset.name] = 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) {
const item_name = item.name.toLowerCase();

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is strictly necessary. After all, https://github.com/EFForg/https-everywhere/pull/14678/files#diff-81d05cf2685349d7bbbe9f394bd74506R188 will simply override this value when the main_frame is loaded.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be necessary when there is no applicable ruleset applied to the main_frame but at least one ruleset applied to the page resources; this is typical when a HTTP-only site serving third-parties HTTPS content.

}
},

getRulesets: function(tabId) {
if (tabId in this.active_tab_rules) {
return this.active_tab_rules[tabId];
if (this.active_tab_rules.has(tabId)) {
return this.active_tab_rules.get(tabId);
} else {
return null;
}
return null;
},

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

getActiveRulesetCount: function (tabId) {
let activeCount = 0;

const rulesets = this.getRulesets(tabId);
if (rulesets) {
for (const ruleset of rulesets) {
if (ruleset.active) {
activeCount++;
}
}
}
return activeCount;
}
};

// FIXME: change this name
var activeRulesets = new AppliedRulesets();
var appliedRulesets = new AppliedRulesets();

var urlBlacklist = new Set();

Expand Down Expand Up @@ -275,7 +300,7 @@ function onBeforeRequest(details) {
}

if (details.type == "main_frame") {
activeRulesets.removeTab(details.tabId);
appliedRulesets.removeTab(details.tabId);
}

var potentiallyApplicable = all_rules.potentiallyApplicableRulesets(canonical_host);
Expand All @@ -291,7 +316,7 @@ function onBeforeRequest(details) {
var newuristr = null;

for (let ruleset of potentiallyApplicable) {
activeRulesets.addRulesetToTab(details.tabId, ruleset);
appliedRulesets.addRulesetToTab(details.tabId, details.type, ruleset);
if (ruleset.active && !newuristr) {
newuristr = ruleset.apply(canonical_url);
}
Expand Down Expand Up @@ -614,13 +639,20 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
} else if (message.type == "delete_from_ruleset_cache") {
all_rules.ruleCache.delete(message.object);
} else if (message.type == "get_active_rulesets") {
sendResponse(activeRulesets.getRulesets(message.object));
sendResponse(appliedRulesets.getRulesets(message.object));
} else if (message.type == "set_ruleset_active_status") {
var ruleset = activeRulesets.getRulesets(message.object.tab_id)[message.object.name];
ruleset.active = message.object.active;
if (ruleset.default_state == message.object.active) {
message.object.active = undefined;
let rulesets = appliedRulesets.getRulesets(message.object.tab_id);

for (let ruleset of rulesets) {
if (ruleset.name == message.object.name) {
ruleset.active = message.object.active;
if (ruleset.default_state == message.object.active) {
message.object.active = undefined;
}
break;
}
}

all_rules.setRuleActiveState(message.object.name, message.object.active).then(() => {
sendResponse(true);
});
Expand Down
18 changes: 11 additions & 7 deletions chromium/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,19 @@ function toggleEnabledDisabled() {
* @param tabArray
*/
function gotTab(activeTab) {
sendMessage("get_active_rulesets", activeTab.id, function(rulesets){
for (var r in rulesets) {
var listDiv = stableRules;
if (!rulesets[r].default_state) {
listDiv = unstableRules;
sendMessage("get_active_rulesets", activeTab.id, function(rulesets) {
if (rulesets) {
for (const ruleset of rulesets) {
let listDiv = stableRules;

if (!ruleset.default_state) {
listDiv = unstableRules;
}
appendRuleLineToListDiv(ruleset, listDiv, activeTab.id);
listDiv.style.display = 'block';
}
appendRuleLineToListDiv(rulesets[r], listDiv, activeTab.id);
listDiv.style.display = 'block';
}

// Only show the "Add a rule" link if we're on an HTTPS page
if (/^https:/.test(activeTab.url)) {
show(e("add-rule-link"));
Expand Down