-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ff78a0
commit 8cc6028
Showing
15 changed files
with
416 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
browser-extension/mv3/src/content-scripts/client/pageScriptMessageListener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { EXTENSION_MESSAGES } from "common/constants"; | ||
|
||
export const initPageScriptMessageListener = () => { | ||
window.addEventListener("message", function (event) { | ||
if (event.source !== window || event.data.source !== "requestly:client") { | ||
return; | ||
} | ||
|
||
switch (event.data.action) { | ||
case "response_rule_applied": | ||
case "request_rule_applied": | ||
// tabId not populated from content script. Popuplate in service worker | ||
chrome.runtime.sendMessage({ | ||
action: EXTENSION_MESSAGES.RULE_EXECUTED, | ||
rule: event.data.rule, | ||
requestDetails: event.data.requestDetails, | ||
}); | ||
break; | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { registerCommands } from "./services/commands"; | ||
import { initContextMenu } from "./services/contextMenu"; | ||
import { initExtensionIcon } from "./services/extensionIcon"; | ||
import { handleInstallUninstall } from "./services/installUninstall"; | ||
import { initMessageHandler } from "./services/messageHandler"; | ||
import { initRulesManager } from "./services/rulesManager"; | ||
import { initWebRequestInterceptor } from "./services/webRequestInterceptor"; | ||
|
||
// initialize | ||
(async () => { | ||
initExtensionIcon(); | ||
registerCommands(); | ||
handleInstallUninstall(); | ||
initRulesManager(); | ||
initMessageHandler(); | ||
initContextMenu(); | ||
initWebRequestInterceptor(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
browser-extension/mv3/src/service-worker/services/extensionIcon.ts
This file was deleted.
Oops, something went wrong.
117 changes: 117 additions & 0 deletions
117
browser-extension/mv3/src/service-worker/services/extensionIconManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { tabService } from "./tabService"; | ||
|
||
interface ExtensionIconConfig { | ||
ruleExecuted?: boolean; | ||
isRecording?: boolean; | ||
} | ||
|
||
class ExtensionIconManager { | ||
#isExtensionDisabled = false; | ||
|
||
#icons = { | ||
DEFAULT: "/resources/images/48x48.png", | ||
DISABLED: "/resources/images/48x48_greyscale.png", | ||
RULE_EXECUTED: "/resources/images/48x48_green.png", | ||
DEFAULT_WITH_REC: "/resources/images/48x48_rec.png", | ||
RULE_EXECUTED_WITH_REC: "/resources/images/48x48_green_rec.png", | ||
}; | ||
|
||
#CONSTANTS = { | ||
PAGE_DATA_ICON_CONFIG: "extensionIconConfig", | ||
}; | ||
|
||
constructor() { | ||
chrome.tabs.onUpdated.addListener((tabId) => { | ||
// FIXME: Can be made better by only listening to url changes on tabs | ||
this.#updateIconState(tabId); | ||
}); | ||
} | ||
|
||
#getDefaultConfig(): ExtensionIconConfig { | ||
return { | ||
ruleExecuted: false, | ||
isRecording: false, | ||
}; | ||
} | ||
|
||
#getIcon(config: ExtensionIconConfig) { | ||
if (this.#isExtensionDisabled) { | ||
return this.#icons.DISABLED; | ||
} | ||
|
||
if (config.ruleExecuted) { | ||
if (config.isRecording) { | ||
return this.#icons.RULE_EXECUTED_WITH_REC; | ||
} | ||
|
||
return this.#icons.RULE_EXECUTED; | ||
} | ||
|
||
if (config.isRecording) { | ||
return this.#icons.DEFAULT_WITH_REC; | ||
} | ||
|
||
return this.#icons.DEFAULT; | ||
} | ||
|
||
#updateIconState(tabId?: number, newConfigKey?: keyof ExtensionIconConfig, newConfigValue?: boolean) { | ||
let config = tabService.getPageData(tabId, this.#CONSTANTS.PAGE_DATA_ICON_CONFIG) || this.#getDefaultConfig(); | ||
|
||
if (newConfigKey && config[newConfigKey] !== newConfigValue) { | ||
config = { ...config, [newConfigKey]: newConfigValue }; | ||
tabService.setPageData(tabId, this.#CONSTANTS.PAGE_DATA_ICON_CONFIG, config); | ||
} | ||
|
||
this.#setExtensionIcon(this.#getIcon(config), tabId); | ||
// tabService.setExtensionIcon(this.#getIcon(config), tabId); | ||
} | ||
|
||
#updateIconStateForAllTabs() { | ||
const tabsMap = tabService.getTabs(); | ||
const tabsWithIconConfig = Object.values(tabsMap).filter((tab) => { | ||
if (tab && tab.pageData) { | ||
return !!tab.pageData[this.#CONSTANTS.PAGE_DATA_ICON_CONFIG]; | ||
} | ||
return false; | ||
}); | ||
tabsWithIconConfig.forEach((tab) => this.#updateIconState(tab.id)); | ||
} | ||
|
||
#setExtensionIcon(path: string, tabId?: number) { | ||
if (tabId === undefined) { | ||
chrome.action.setIcon({ path }); | ||
} else { | ||
chrome.action.setIcon({ path, tabId }); | ||
} | ||
} | ||
|
||
markExtensionEnabled = () => { | ||
this.#isExtensionDisabled = false; | ||
this.#setExtensionIcon(this.#icons.DEFAULT); | ||
// tabService.setExtensionIcon(this.#icons.DEFAULT); | ||
this.#updateIconStateForAllTabs(); | ||
}; | ||
|
||
markExtensionDisabled = () => { | ||
this.#isExtensionDisabled = true; | ||
this.#setExtensionIcon(this.#icons.DISABLED); | ||
// tabService.setExtensionIcon(this.#icons.DISABLED); | ||
this.#updateIconStateForAllTabs(); | ||
}; | ||
|
||
markRuleExecuted(tabId: number) { | ||
this.#updateIconState(tabId, "ruleExecuted", true); | ||
} | ||
|
||
markRecording(tabId: number) { | ||
this.#updateIconState(tabId, "isRecording", true); | ||
} | ||
|
||
markNotRecording(tabId: number) { | ||
this.#updateIconState(tabId, "isRecording", false); | ||
} | ||
} | ||
|
||
const extensionIconManager = new ExtensionIconManager(); | ||
|
||
export default extensionIconManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
browser-extension/mv3/src/service-worker/services/ruleExecutionHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Rule } from "common/types"; | ||
import extensionIconManager from "./extensionIconManager"; | ||
import { DataScope, TAB_SERVICE_DATA, tabService } from "./tabService"; | ||
import { getRecords } from "common/storage"; | ||
|
||
interface RulesExecutionLog { | ||
ruleId: string; | ||
requestDetails: chrome.webRequest.WebRequestDetails | chrome.webRequest.WebResponseDetails; | ||
} | ||
|
||
class RuleExecutionHandler { | ||
constructor() {} | ||
|
||
getExecutedRules = async (tabId: number) => { | ||
const rulesExecutionLogs: RulesExecutionLog[] = | ||
tabService.getPageData(tabId, TAB_SERVICE_DATA.RULES_EXECUTION_LOGS, []) || []; | ||
|
||
const appliedRuleIds = rulesExecutionLogs.map((executionLog) => executionLog.ruleId).filter((id) => !!id); | ||
const uniqueAppliedRuleIds = Array.from(new Set(appliedRuleIds)); | ||
const appliedRules = await getRecords(uniqueAppliedRuleIds); | ||
return appliedRules; | ||
}; | ||
|
||
processTabCachedRulesExecutions = (tabId: number) => { | ||
const rulesExecutionLogs: RulesExecutionLog[] = | ||
tabService.getData(tabId, TAB_SERVICE_DATA.RULES_EXECUTION_LOGS, []) || []; | ||
|
||
rulesExecutionLogs.forEach((executionLog) => { | ||
this.onRuleExecuted({ id: executionLog.ruleId } as Rule, executionLog.requestDetails); | ||
}); | ||
|
||
tabService.removeData(tabId, TAB_SERVICE_DATA.RULES_EXECUTION_LOGS); | ||
}; | ||
|
||
onRuleExecuted = ( | ||
rule: Rule, | ||
requestDetails: chrome.webRequest.WebRequestDetails | chrome.webRequest.WebResponseDetails, | ||
isMainFrameRequest?: boolean | ||
) => { | ||
const tabDataScope = isMainFrameRequest ? DataScope.TAB : DataScope.PAGE; | ||
|
||
extensionIconManager.markRuleExecuted(requestDetails.tabId); | ||
|
||
const rulesExecutionLogs: RulesExecutionLog[] = | ||
tabService.getDataForScope(tabDataScope, requestDetails.tabId, TAB_SERVICE_DATA.RULES_EXECUTION_LOGS, []) || []; | ||
rulesExecutionLogs.push({ ruleId: rule.id, requestDetails }); | ||
tabService.setDataForScope( | ||
tabDataScope, | ||
requestDetails.tabId, | ||
TAB_SERVICE_DATA.RULES_EXECUTION_LOGS, | ||
rulesExecutionLogs | ||
); | ||
}; | ||
} | ||
|
||
const ruleExecutionHandler = new RuleExecutionHandler(); | ||
export default ruleExecutionHandler; |
Oops, something went wrong.