Add prettier config and reformat all files
We are using this file as a template as we do for main Gerrit:
https://github.com/google/gts/blob/main/.prettierrc.json
Change-Id: I8c61ebf209f998943fbf0e2df13b9d38a4e5a934
diff --git a/.prettierrc b/.prettierrc
new file mode 100644
index 0000000..ab46bc1
--- /dev/null
+++ b/.prettierrc
@@ -0,0 +1,8 @@
+{
+ "tabWidth": 2,
+ "useTabs": false,
+ "bracketSpacing": false,
+ "singleQuote": true,
+ "trailingComma": "es5",
+ "arrowParens": "avoid"
+}
\ No newline at end of file
diff --git a/src/content_script.ts b/src/content_script.ts
index 19245a7..1cb33da 100644
--- a/src/content_script.ts
+++ b/src/content_script.ts
@@ -1,4 +1,4 @@
-import { isInjectRule, Operator, Rule, getUrlParameter } from './utils';
+import {isInjectRule, Operator, Rule, getUrlParameter} from './utils';
declare global {
interface Window {
@@ -17,17 +17,23 @@
// Wait at most 5s before considering it as loaded.
const MAX_WAIT_TIME = 5000;
const getHeaderEl = () => {
- if (document.querySelector("#app")) {
- return document.querySelector("#app").shadowRoot.querySelector("#app-element").shadowRoot.querySelector("#mainHeader");
+ if (document.querySelector('#app')) {
+ return document
+ .querySelector('#app')
+ .shadowRoot.querySelector('#app-element')
+ .shadowRoot.querySelector('#mainHeader');
} else {
- return document.querySelector("gr-app").shadowRoot.querySelector("gr-app-element").shadowRoot.querySelector("gr-main-header");
+ return document
+ .querySelector('gr-app')
+ .shadowRoot.querySelector('gr-app-element')
+ .shadowRoot.querySelector('gr-main-header');
}
-}
+};
const onGerritReady = async () => {
let header;
try {
header = getHeaderEl();
- } catch (e) { }
+ } catch (e) {}
let waitTime = 0;
while (!header) {
@@ -36,10 +42,10 @@
await nextTick(1000);
try {
header = getHeaderEl();
- } catch (e) { }
+ } catch (e) {}
}
return true;
-}
+};
let numOfSnackBars = 0;
function createSnackBar(message: string) {
@@ -58,10 +64,10 @@
}
// Apply injection rules to Gerrit sites if enabled
-chrome.runtime.sendMessage({ type: 'isEnabled' }, (isEnabled) => {
+chrome.runtime.sendMessage({type: 'isEnabled'}, isEnabled => {
if (!isEnabled) return;
- chrome.storage.sync.get(['rules'], (result) => {
+ chrome.storage.sync.get(['rules'], result => {
if (!result['rules']) return;
const rules = result['rules'] as Rule[];
@@ -101,38 +107,54 @@
} else if (rule.operator === Operator.INJECT_EXP) {
const exps = getUrlParameter('experiment');
const hasSeachString = !!window.location.search;
- const injectedExpNotInExps = new Set(rule.destination.trim().split(',').filter(exp => !exps.includes(exp.trim())));
+ const injectedExpNotInExps = new Set(
+ rule.destination
+ .trim()
+ .split(',')
+ .filter(exp => !exps.includes(exp.trim()))
+ );
if (injectedExpNotInExps.size) {
- const addedParams = [...injectedExpNotInExps].reduce((str, exp) => str += `experiment=${exp}&`, '');
- window.location.href += hasSeachString ? `&${addedParams}` : `?${addedParams}`;
+ const addedParams = [...injectedExpNotInExps].reduce(
+ (str, exp) => (str += `experiment=${exp}&`),
+ ''
+ );
+ window.location.href += hasSeachString
+ ? `&${addedParams}`
+ : `?${addedParams}`;
}
}
});
// test redirect rules
- rules.filter(rule => !isInjectRule(rule)).forEach(rule => {
- if (rule.operator === Operator.REDIRECT
- && !rule.disabled
- // only test for js/html
- && /\.(js|html)+$/.test(rule.destination)
- ) {
- fetch(rule.destination).then(res => {
- if (res.status < 200 || res.status >= 300) throw new Error("Resource not found");
- }).catch(e => {
- const errorSnack = createSnackBar(
- `You may have an invalid redirect rule from ${rule.target} to ${rule.destination}`
- );
+ rules
+ .filter(rule => !isInjectRule(rule))
+ .forEach(rule => {
+ if (
+ rule.operator === Operator.REDIRECT &&
+ !rule.disabled &&
+ // only test for js/html
+ /\.(js|html)+$/.test(rule.destination)
+ ) {
+ fetch(rule.destination)
+ .then(res => {
+ if (res.status < 200 || res.status >= 300)
+ throw new Error('Resource not found');
+ })
+ .catch(e => {
+ const errorSnack = createSnackBar(
+ `You may have an invalid redirect rule from ${rule.target} to ${rule.destination}`
+ );
- // in case body is unresolved
- document.body.style.display = "block";
- document.body.style.opacity = "1";
+ // in case body is unresolved
+ document.body.style.display = 'block';
+ document.body.style.opacity = '1';
- setTimeout(() => {
- errorSnack.remove();
- numOfSnackBars--;
- }, 10 * 1000);
- });
- }
- });
+ setTimeout(() => {
+ errorSnack.remove();
+ numOfSnackBars--;
+ }, 10 * 1000);
+ });
+ }
+ });
});
-});
\ No newline at end of file
+});
diff --git a/src/popup.ts b/src/popup.ts
index e2484dc..be78b64 100644
--- a/src/popup.ts
+++ b/src/popup.ts
@@ -57,18 +57,21 @@
}
private loadRules() {
- chrome.storage.sync.get(['rules', 'enabled'], (result) => {
+ chrome.storage.sync.get(['rules', 'enabled'], result => {
if (!result['rules']) return;
- this.rules =
- (result['rules'] as Rule[]).map(rule => (rule.isNew = false, rule));
+ this.rules = (result['rules'] as Rule[]).map(
+ rule => ((rule.isNew = false), rule)
+ );
this.rulesStr = JSON.stringify(this.rules, null, 2);
});
}
saveRules() {
chrome.storage.sync.set({rules: this.rules.slice()});
- chrome.runtime.sendMessage(
- {type: 'updateRules', rules: this.rules.slice()});
+ chrome.runtime.sendMessage({
+ type: 'updateRules',
+ rules: this.rules.slice(),
+ });
this.refresh();
}
@@ -83,7 +86,7 @@
this.rulesStr = JSON.stringify(this.rules, null, 2);
window.localStorage.removeItem('helper-announcement');
this.requestUpdate();
- })
+ });
}
onRuleDeletion(event: CustomEvent<Rule>) {
@@ -113,7 +116,7 @@
private refresh(runBefore?: (tab: chrome.tabs.Tab) => null) {
// refresh the tab now
- chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
+ chrome.tabs.query({active: true, currentWindow: true}, tabs => {
if (!tabs[0] || !tabs[0].id) return;
if (runBefore) runBefore(tabs[0]);
chrome.tabs.update(tabs[0].id, {url: tabs[0].url});
@@ -144,8 +147,9 @@
}
exportRules() {
- const dataStr = 'data:text/json;charset=utf-8,' +
- encodeURIComponent(JSON.stringify(this.rules));
+ const dataStr =
+ 'data:text/json;charset=utf-8,' +
+ encodeURIComponent(JSON.stringify(this.rules));
const downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute('href', dataStr);
downloadAnchorNode.setAttribute('download', 'rules.json');
@@ -155,85 +159,87 @@
render() {
return html`
- <p ?hidden=${!this.announcementText}>${this.announcementText}</p>
- <header>
- <button class="primary" @click=${this.disableHelper}>Disable</button>
- </header>
- <div ?hidden=${this.isImport}>
- <gdh-rule-set
- @enable-me-only=${this.enableMeOnly}
- @rule-deleted=${this.onRuleDeletion}
- @rule-changed=${this.onRuleChanged}
- .rules=${this.rules}>
- </gdh-rule-set>
- <div class="buttons">
- <button class="primary" @click=${this.saveRules}>Save</button>
- <button @click=${this.addNewRule}>Add</button>
- <button @click=${this.resetRules}>Reset</button>
- <button @click=${this.startImport}>Import</button>
- <button @click=${this.exportRules}>Export</button>
- </div>
+ <p ?hidden=${!this.announcementText}>${this.announcementText}</p>
+ <header>
+ <button class="primary" @click=${this.disableHelper}>Disable</button>
+ </header>
+ <div ?hidden=${this.isImport}>
+ <gdh-rule-set
+ @enable-me-only=${this.enableMeOnly}
+ @rule-deleted=${this.onRuleDeletion}
+ @rule-changed=${this.onRuleChanged}
+ .rules=${this.rules}
+ >
+ </gdh-rule-set>
+ <div class="buttons">
+ <button class="primary" @click=${this.saveRules}>Save</button>
+ <button @click=${this.addNewRule}>Add</button>
+ <button @click=${this.resetRules}>Reset</button>
+ <button @click=${this.startImport}>Import</button>
+ <button @click=${this.exportRules}>Export</button>
</div>
- <div ?hidden=${!this.isImport}>
- <p ?hidden=${!this.importError}>${this.importError}</p>
- <textarea
- name="rules"
- .value=${this.rulesStr}
- @input=${this.handleRulesInputChange}>
- </textarea>
- <div class="buttons">
- <button @click=${this.confirmImport}>Import</button>
- <button @click=${this.cancelImport}>Cancel</button>
- <button @click=${this.resetRules}>Reset</button>
- </div>
+ </div>
+ <div ?hidden=${!this.isImport}>
+ <p ?hidden=${!this.importError}>${this.importError}</p>
+ <textarea
+ name="rules"
+ .value=${this.rulesStr}
+ @input=${this.handleRulesInputChange}
+ >
+ </textarea>
+ <div class="buttons">
+ <button @click=${this.confirmImport}>Import</button>
+ <button @click=${this.cancelImport}>Cancel</button>
+ <button @click=${this.resetRules}>Reset</button>
</div>
- `;
+ </div>
+ `;
}
static get styles() {
return css`
- :host {
- display: block;
- padding: 10px;
- }
- p {
- background: yellow;
- padding: 10px;
- }
- button {
- color: #1565c0;
- background: transparent;
- font-family: Roboto, sans-serif;
- font-size: 14px;
- font-weight: 400;
- text-transform: uppercase;
- user-select: none;
- box-sizing: content-box;
- border-radius: 4px;
- border: none;
- padding: 4px 8px;
- cursor: pointer;
- outline: none;
- }
- button:hover {
- background-color: #f4f0fa;
- }
- button.primary {
- color: white;
- background-color: #1565c0;
- }
- button.primary:hover {
- background-color: #2575d0;
- }
- header {
- display: flex;
- flex-direction: row-reverse;
- }
- textarea {
- min-width: 500px;
- min-height: 500px;
- }
- `;
+ :host {
+ display: block;
+ padding: 10px;
+ }
+ p {
+ background: yellow;
+ padding: 10px;
+ }
+ button {
+ color: #1565c0;
+ background: transparent;
+ font-family: Roboto, sans-serif;
+ font-size: 14px;
+ font-weight: 400;
+ text-transform: uppercase;
+ user-select: none;
+ box-sizing: content-box;
+ border-radius: 4px;
+ border: none;
+ padding: 4px 8px;
+ cursor: pointer;
+ outline: none;
+ }
+ button:hover {
+ background-color: #f4f0fa;
+ }
+ button.primary {
+ color: white;
+ background-color: #1565c0;
+ }
+ button.primary:hover {
+ background-color: #2575d0;
+ }
+ header {
+ display: flex;
+ flex-direction: row-reverse;
+ }
+ textarea {
+ min-width: 500px;
+ min-height: 500px;
+ }
+ `;
}
}
@@ -246,62 +252,61 @@
render() {
return html`
- <ul>
- <li class="header">
- <span></span>
- <span>Target</span>
- <span>Operator</span>
- <span>Destination</span>
- <span></span>
- </li>
- ${
- this.rules.map(
- rule =>
- html`<li><gdh-rule-item .rule=${rule}></gdh-rule-item></li>`)}
- </ul>
- `;
+ <ul>
+ <li class="header">
+ <span></span>
+ <span>Target</span>
+ <span>Operator</span>
+ <span>Destination</span>
+ <span></span>
+ </li>
+ ${this.rules.map(
+ rule => html`<li><gdh-rule-item .rule=${rule}></gdh-rule-item></li>`
+ )}
+ </ul>
+ `;
}
static get styles() {
return css`
- :host {
- display: block;
- position: relative;
- padding: 16px 0;
- }
- ul {
- list-style: none;
- margin: 0;
- padding: 0;
- }
- ul li {
- margin: 5px 0;
- }
- ul li.header {
- display: flex;
- }
- li.header span {
- text-align: center;
- font-weight: bold;
- }
- li.header span:nth-child(1) {
- flex-basis: 20px;
- }
- li.header span:nth-child(2) {
- flex: 1;
- flex-basis: 220px;
- }
- li.header span:nth-child(3) {
- flex-basis: 140px;
- }
- li.header span:nth-child(4) {
- flex: 1;
- flex-basis: 220px;
- }
- li.header span:nth-child(5) {
- flex-basis: 20px;
- }
- `;
+ :host {
+ display: block;
+ position: relative;
+ padding: 16px 0;
+ }
+ ul {
+ list-style: none;
+ margin: 0;
+ padding: 0;
+ }
+ ul li {
+ margin: 5px 0;
+ }
+ ul li.header {
+ display: flex;
+ }
+ li.header span {
+ text-align: center;
+ font-weight: bold;
+ }
+ li.header span:nth-child(1) {
+ flex-basis: 20px;
+ }
+ li.header span:nth-child(2) {
+ flex: 1;
+ flex-basis: 220px;
+ }
+ li.header span:nth-child(3) {
+ flex-basis: 140px;
+ }
+ li.header span:nth-child(4) {
+ flex: 1;
+ flex-basis: 220px;
+ }
+ li.header span:nth-child(5) {
+ flex-basis: 20px;
+ }
+ `;
}
}
@@ -342,64 +347,76 @@
}
onRuleDeletion(rule: Rule) {
- this.dispatchEvent(new CustomEvent<Rule>('rule-deleted', {
- detail: rule,
- bubbles: true,
- composed: true,
- }));
+ this.dispatchEvent(
+ new CustomEvent<Rule>('rule-deleted', {
+ detail: rule,
+ bubbles: true,
+ composed: true,
+ })
+ );
}
toggleDisable(e: KeyboardEvent) {
this.rule.disabled = !this.rule.disabled;
// notify the change
- this.dispatchEvent(new CustomEvent<Rule>('rule-changed', {
- detail: this.rule,
- bubbles: true,
- composed: true,
- }));
+ this.dispatchEvent(
+ new CustomEvent<Rule>('rule-changed', {
+ detail: this.rule,
+ bubbles: true,
+ composed: true,
+ })
+ );
}
enableOnlyMe(e: KeyboardEvent) {
- this.dispatchEvent(new CustomEvent<Rule>('enable-me-only', {
- detail: this.rule,
- bubbles: true,
- composed: true,
- }));
+ this.dispatchEvent(
+ new CustomEvent<Rule>('enable-me-only', {
+ detail: this.rule,
+ bubbles: true,
+ composed: true,
+ })
+ );
}
render() {
return html`
- <div class="checkboxContainer">
- <input
- type="checkbox"
- .checked=${!this.rule.disabled}
- @dblclick=${this.enableOnlyMe}
- @click=${this.toggleDisable} />
- </div>
+ <div class="checkboxContainer">
<input
- class="target"
- .disabled=${isInjectRule(this.rule)}
- type="text"
- .value=${this.rule.target}
- @input=${this.handleInputOnTarget} />
- <gdh-dropdown
- .selectedIndex=${this.operators.indexOf(this.rule.operator)}
- @select-changed=${this.onSelectedChange}
- .items=${this.operators}>
- </gdh-dropdown>
- <textarea
- name="destination"
- .value=${this.rule.destination}
- @input=${this.handleInputOnDestination}
- .disabled=${this.rule.operator === Operator.BLOCK}>
- </textarea>
- <div class="deleteContainer">
- <span class="icon deleteButton"
- @click=${this.onRuleDeletion.bind(this, this.rule)}>
- delete
- </span>
- </div>
- `;
+ type="checkbox"
+ .checked=${!this.rule.disabled}
+ @dblclick=${this.enableOnlyMe}
+ @click=${this.toggleDisable}
+ />
+ </div>
+ <input
+ class="target"
+ .disabled=${isInjectRule(this.rule)}
+ type="text"
+ .value=${this.rule.target}
+ @input=${this.handleInputOnTarget}
+ />
+ <gdh-dropdown
+ .selectedIndex=${this.operators.indexOf(this.rule.operator)}
+ @select-changed=${this.onSelectedChange}
+ .items=${this.operators}
+ >
+ </gdh-dropdown>
+ <textarea
+ name="destination"
+ .value=${this.rule.destination}
+ @input=${this.handleInputOnDestination}
+ .disabled=${this.rule.operator === Operator.BLOCK}
+ >
+ </textarea>
+ <div class="deleteContainer">
+ <span
+ class="icon deleteButton"
+ @click=${this.onRuleDeletion.bind(this, this.rule)}
+ >
+ delete
+ </span>
+ </div>
+ `;
}
static get styles() {
@@ -431,11 +448,11 @@
justify-content: center;
margin-right: 8px;
}
- input[type="checkbox"] {
+ input[type='checkbox'] {
width: 16px;
height: 16px;
}
- input[type="text"] {
+ input[type='text'] {
font-family: Roboto, sans-serif;
font-size: 13px;
}
@@ -443,7 +460,7 @@
font-family: 'Roboto Mono', monospace;
font-size: 12px;
}
- input[type="text"],
+ input[type='text'],
textarea {
flex: 1;
min-width: 220px;
@@ -451,7 +468,7 @@
border: none;
border-bottom: 1px solid #ccc;
}
- input[type="text"]:focus,
+ input[type='text']:focus,
textarea:focus {
border-bottom: 1px solid #3c88fd;
background: #e8f0fe;
@@ -460,7 +477,7 @@
textarea[disabled] {
background: #e8eaed;
}
- `
+ `,
];
}
}
@@ -476,11 +493,13 @@
handleSelect(idx: number) {
this.selectedIndex = idx;
- this.dispatchEvent(new CustomEvent<number>('select-changed', {
- detail: idx,
- bubbles: true,
- composed: true,
- }));
+ this.dispatchEvent(
+ new CustomEvent<number>('select-changed', {
+ detail: idx,
+ bubbles: true,
+ composed: true,
+ })
+ );
this.isVisible = false;
}
@@ -490,25 +509,29 @@
render() {
return html`
- <label for="trigger" @click=${this.toggleVisible}>
- <div class="selected-value">${this.items[this.selectedIndex]}</div>
- <input
- .checked=${this.isVisible}
- @input=${this.toggleVisible}
- type="checkbox"
- name="trigger" />
- <ul class="options">
- ${this.items.map((item, i) => html`
- <li
- @click=${this.handleSelect.bind(this, i)}
- class="${i === this.selectedIndex ? 'active' : ''}">
- ${item}
- </li>
- `)}
- </ul>
- <span class="icon">expand_more</span>
- </label>
- `;
+ <label for="trigger" @click=${this.toggleVisible}>
+ <div class="selected-value">${this.items[this.selectedIndex]}</div>
+ <input
+ .checked=${this.isVisible}
+ @input=${this.toggleVisible}
+ type="checkbox"
+ name="trigger"
+ />
+ <ul class="options">
+ ${this.items.map(
+ (item, i) => html`
+ <li
+ @click=${this.handleSelect.bind(this, i)}
+ class="${i === this.selectedIndex ? 'active' : ''}"
+ >
+ ${item}
+ </li>
+ `
+ )}
+ </ul>
+ <span class="icon">expand_more</span>
+ </label>
+ `;
}
static get styles() {
@@ -554,10 +577,11 @@
width: 100%;
height: 100%;
cursor: pointer;
- }
+ }
input:checked + ul {
display: block;
- box-shadow: 0 2px 1px -1px rgba(0, 0, 0, 0.2),0 1px 1px 0 rgba(0, 0, 0, 0.14),0 1px 3px 0 rgba(0,0,0,.12);
+ box-shadow: 0 2px 1px -1px rgba(0, 0, 0, 0.2),
+ 0 1px 1px 0 rgba(0, 0, 0, 0.14), 0 1px 3px 0 rgba(0, 0, 0, 0.12);
}
input:checked + ul li:hover {
background: #ddd;
@@ -574,7 +598,7 @@
transform: rotate(-135deg);
-webkit-transform: rotate(-135deg);
}
- `
+ `,
];
}
}
diff --git a/src/service_worker.ts b/src/service_worker.ts
index 549e7ab..3e068c2 100644
--- a/src/service_worker.ts
+++ b/src/service_worker.ts
@@ -1,4 +1,10 @@
-import {DEFAULT_RULES, isInjectRule, isValidRule, Operator, Rule} from './utils';
+import {
+ DEFAULT_RULES,
+ isInjectRule,
+ isValidRule,
+ Operator,
+ Rule,
+} from './utils';
let rules: Rule[] = [...DEFAULT_RULES];
@@ -11,7 +17,7 @@
// Load default configs and states when install
chrome.runtime.onInstalled.addListener(() => {
- chrome.storage.sync.get(['rules'], (res) => {
+ chrome.storage.sync.get(['rules'], res => {
const existingRules: Rule[] = res['rules'] || [];
const validRules = existingRules.filter(isValidRule);
if (!validRules.length) {
@@ -52,29 +58,38 @@
if (!TabStates.has(resp.tabId)) {
return {responseHeaders: resp.responseHeaders};
}
- const matches = rules.filter(isValidRule)
- .filter(
- rule => rule.operator === Operator.REMOVE_RESPONSE_HEADER
- && !rule.disabled
- && new RegExp(rule.target).test(resp.url));
+ const matches = rules
+ .filter(isValidRule)
+ .filter(
+ rule =>
+ rule.operator === Operator.REMOVE_RESPONSE_HEADER &&
+ !rule.disabled &&
+ new RegExp(rule.target).test(resp.url)
+ );
matches.forEach(rule => {
- const removedHeaders = rule.destination.split(",").map(name => name.toLowerCase());
- resp.responseHeaders = resp.responseHeaders
- .filter(h => !removedHeaders.includes(h.name.toLowerCase()));
+ const removedHeaders = rule.destination
+ .split(',')
+ .map(name => name.toLowerCase());
+ resp.responseHeaders = resp.responseHeaders.filter(
+ h => !removedHeaders.includes(h.name.toLowerCase())
+ );
});
- const addMatches = rules.filter(isValidRule)
- .filter(
- rule => rule.operator === Operator.ADD_RESPONSE_HEADER
- && !rule.disabled
- && new RegExp(rule.target).test(resp.url));
+ const addMatches = rules
+ .filter(isValidRule)
+ .filter(
+ rule =>
+ rule.operator === Operator.ADD_RESPONSE_HEADER &&
+ !rule.disabled &&
+ new RegExp(rule.target).test(resp.url)
+ );
addMatches.forEach(rule => {
- const addedHeaders = rule.destination.split("|")
+ const addedHeaders = rule.destination.split('|');
addedHeaders.forEach(addedHeader => {
- const partial = addedHeader.split("=");
+ const partial = addedHeader.split('=');
if (partial.length === 2) {
resp.responseHeaders.push({
- 'name': partial[0],
- 'value': partial[1]
+ name: partial[0],
+ value: partial[1],
});
}
});
@@ -87,13 +102,19 @@
return {cancel: false};
}
- const matches = rules.filter(isValidRule)
- .filter(
- rule => !isInjectRule(rule) && !rule.disabled &&
- new RegExp(rule.target).test(details.url));
+ const matches = rules
+ .filter(isValidRule)
+ .filter(
+ rule =>
+ !isInjectRule(rule) &&
+ !rule.disabled &&
+ new RegExp(rule.target).test(details.url)
+ );
const blockMatch = matches.find(rule => rule.operator === Operator.BLOCK);
- const redirectMatch = matches.find(rule => rule.operator === Operator.REDIRECT);
+ const redirectMatch = matches.find(
+ rule => rule.operator === Operator.REDIRECT
+ );
// block match takes highest priority
if (blockMatch) {
@@ -103,9 +124,11 @@
// then redirect
if (redirectMatch) {
return {
- redirectUrl:
- details.url.replace(new RegExp(redirectMatch.target), redirectMatch.destination),
- };
+ redirectUrl: details.url.replace(
+ new RegExp(redirectMatch.target),
+ redirectMatch.destination
+ ),
+ };
}
// otherwise, don't do anything
@@ -113,7 +136,8 @@
}
function onBeforeSendHeaders(
- details: chrome.webRequest.WebRequestHeadersDetails) {
+ details: chrome.webRequest.WebRequestHeadersDetails
+) {
if (!details || !details.requestHeaders) return {};
if (!TabStates.has(details.tabId)) {
@@ -124,32 +148,37 @@
let added = false;
while (--len) {
const header = details.requestHeaders[len];
- if (header.name.toLowerCase() === 'cache-control' ||
- header.name.toLowerCase() === 'x-google-cache-control') {
+ if (
+ header.name.toLowerCase() === 'cache-control' ||
+ header.name.toLowerCase() === 'x-google-cache-control'
+ ) {
header.value = 'max-age=0, no-cache, no-store, must-revalidate';
added = true;
}
}
if (!added) {
details.requestHeaders.push({
- 'name': 'Cache-Control',
- 'value': 'max-age=0, no-cache, no-store, must-revalidate'
+ name: 'Cache-Control',
+ value: 'max-age=0, no-cache, no-store, must-revalidate',
});
}
- const matches = rules.filter(isValidRule)
- .filter(
- rule => rule.operator === Operator.ADD_REQUEST_HEADER
- && !rule.disabled
- && new RegExp(rule.target).test(details.url));
+ const matches = rules
+ .filter(isValidRule)
+ .filter(
+ rule =>
+ rule.operator === Operator.ADD_REQUEST_HEADER &&
+ !rule.disabled &&
+ new RegExp(rule.target).test(details.url)
+ );
matches.forEach(rule => {
- const addedHeaders = rule.destination.split(",")
+ const addedHeaders = rule.destination.split(',');
addedHeaders.forEach(addedHeader => {
- const partial = addedHeader.split("=");
+ const partial = addedHeader.split('=');
if (partial.length === 2) {
details.requestHeaders.push({
- 'name': partial[0],
- 'value': partial[1]
+ name: partial[0],
+ value: partial[1],
});
}
});
@@ -169,17 +198,24 @@
// in case any listeners already set up, remove them first
removeListeners();
chrome.webRequest.onHeadersReceived.addListener(
- onHeadersReceived, {urls: ['<all_urls>']},
- ['blocking', 'responseHeaders']);
+ onHeadersReceived,
+ {urls: ['<all_urls>']},
+ ['blocking', 'responseHeaders']
+ );
// blocking or redirecting
chrome.webRequest.onBeforeRequest.addListener(
- onBeforeRequest, {urls: ['<all_urls>']}, ['blocking', 'extraHeaders']);
+ onBeforeRequest,
+ {urls: ['<all_urls>']},
+ ['blocking', 'extraHeaders']
+ );
// disabling cache
chrome.webRequest.onBeforeSendHeaders.addListener(
- onBeforeSendHeaders, {urls: ['<all_urls>']},
- ['blocking', 'requestHeaders']);
+ onBeforeSendHeaders,
+ {urls: ['<all_urls>']},
+ ['blocking', 'requestHeaders']
+ );
}
function removeListeners() {
@@ -217,23 +253,23 @@
}
}
-chrome.browserAction.onClicked.addListener((tab) => {
- if (TabStates.has(tab.id)) {
- // enable -> disable
- disableHelper(tab);
- } else {
- // disable -> enable
- enableHelper(tab);
- if (lastFocusedWindow) {
- chrome.tabs.update(lastFocusedWindow.id!, {url: lastFocusedWindow.url});
- }
+chrome.browserAction.onClicked.addListener(tab => {
+ if (TabStates.has(tab.id)) {
+ // enable -> disable
+ disableHelper(tab);
+ } else {
+ // disable -> enable
+ enableHelper(tab);
+ if (lastFocusedWindow) {
+ chrome.tabs.update(lastFocusedWindow.id!, {url: lastFocusedWindow.url});
}
+ }
});
// Enable / disable the helper based on state of this tab
// This will be called when tab was activated
function checkCurrentTab() {
- chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, ([tab]) => {
+ chrome.tabs.query({active: true, lastFocusedWindow: true}, ([tab]) => {
if (lastFocusedWindow === tab) return;
if (!tab || !tab.url) return;
@@ -246,7 +282,7 @@
lastFocusedWindow = tab;
// read the latest states and rules
- chrome.storage.sync.get(['rules'], (res) => {
+ chrome.storage.sync.get(['rules'], res => {
rules = res['rules'] || [];
});
});
diff --git a/src/utils.ts b/src/utils.ts
index 9d3ff96..b990a63 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -1,4 +1,4 @@
-import * as _DEFAULT_RULES from "../data/rules.json";
+import * as _DEFAULT_RULES from '../data/rules.json';
/**
* Default rules.
@@ -9,29 +9,33 @@
* Retrieves default rules from remote rules file, fallback to existing DEFAULT_RULES
*/
export async function getDefaultRules() {
- // try fetch from remote
- const remoteRulesUrl = 'https://gerrit.googlesource.com/gerrit-fe-dev-helper/+/refs/heads/master/data/rules.json?format=TEXT';
- try {
- const response = await fetch(remoteRulesUrl)
- const encodedText = await response.text();
- return JSON.parse(atob(encodedText));
- } catch(e) {
- console.log(e);
- }
+ // try fetch from remote
+ const remoteRulesUrl =
+ 'https://gerrit.googlesource.com/gerrit-fe-dev-helper/+/refs/heads/master/data/rules.json?format=TEXT';
+ try {
+ const response = await fetch(remoteRulesUrl);
+ const encodedText = await response.text();
+ return JSON.parse(atob(encodedText));
+ } catch (e) {
+ console.log(e);
+ }
- // fallback to existing default rules
- return DEFAULT_RULES;
+ // fallback to existing default rules
+ return DEFAULT_RULES;
}
/**
* Returns if it's a valid rule (syntax only).
*/
export function isValidRule(rule: Rule) {
- return Object.values(Operator).includes(rule.operator) &&
+ return (
+ Object.values(Operator).includes(rule.operator) &&
((rule.operator === Operator.BLOCK && rule.target) ||
- (rule.operator === Operator.REDIRECT && rule.target &&
+ (rule.operator === Operator.REDIRECT &&
+ rule.target &&
rule.destination) ||
- !!rule.destination);
+ !!rule.destination)
+ );
}
/**
@@ -39,8 +43,12 @@
*/
export function isInjectRule(rule: Rule) {
return [
- Operator.INJECT_JS_MODULE_PLUGIN, Operator.INJECT_JS_PLUGIN, Operator.INJECT_HTML_CODE,
- Operator.INJECT_HTML_PLUGIN, Operator.INJECT_JS_CODE, Operator.INJECT_EXP
+ Operator.INJECT_JS_MODULE_PLUGIN,
+ Operator.INJECT_JS_PLUGIN,
+ Operator.INJECT_HTML_CODE,
+ Operator.INJECT_HTML_PLUGIN,
+ Operator.INJECT_JS_CODE,
+ Operator.INJECT_EXP,
].some(op => op === rule.operator);
}
@@ -86,4 +94,4 @@
}
}
return res;
-}
\ No newline at end of file
+}
diff --git a/tsconfig.json b/tsconfig.json
index 83d3156..fa50d3f 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -1,13 +1,13 @@
{
- "compilerOptions": {
- "module": "commonjs",
- "target": "es6",
- "resolveJsonModule": true,
- "noImplicitAny": false,
- "experimentalDecorators": true,
- "sourceMap": true,
- "outDir": "dist",
- "noEmitOnError": true,
- "typeRoots": [ "node_modules/@types" ]
- },
-}
\ No newline at end of file
+ "compilerOptions": {
+ "module": "commonjs",
+ "target": "es6",
+ "resolveJsonModule": true,
+ "noImplicitAny": false,
+ "experimentalDecorators": true,
+ "sourceMap": true,
+ "outDir": "dist",
+ "noEmitOnError": true,
+ "typeRoots": ["node_modules/@types"]
+ }
+}
diff --git a/webpack/webpack.config.js b/webpack/webpack.config.js
index 2e6be53..4bf36a5 100644
--- a/webpack/webpack.config.js
+++ b/webpack/webpack.config.js
@@ -1,32 +1,32 @@
const path = require('path');
const srcDir = '../src/';
-const MODE = "production";
+const MODE = 'production';
module.exports = {
- mode: MODE,
- entry: {
- popup: path.join(__dirname, srcDir + 'popup.ts'),
- service_worker: path.join(__dirname, srcDir + 'service_worker.ts'),
- content_script: path.join(__dirname, srcDir + 'content_script.ts')
- },
- optimization:{
- minimize: MODE === "production",
- },
- output: {
- path: path.join(__dirname, '../dist/'),
- filename: '[name].js'
- },
- module: {
- rules: [
- {
- test: /\.ts$/,
- use: 'ts-loader',
- exclude: /node_modules/
- }
- ]
- },
- resolve: {
- extensions: ['.ts', '.js']
- }
-};
\ No newline at end of file
+ mode: MODE,
+ entry: {
+ popup: path.join(__dirname, srcDir + 'popup.ts'),
+ service_worker: path.join(__dirname, srcDir + 'service_worker.ts'),
+ content_script: path.join(__dirname, srcDir + 'content_script.ts'),
+ },
+ optimization: {
+ minimize: MODE === 'production',
+ },
+ output: {
+ path: path.join(__dirname, '../dist/'),
+ filename: '[name].js',
+ },
+ module: {
+ rules: [
+ {
+ test: /\.ts$/,
+ use: 'ts-loader',
+ exclude: /node_modules/,
+ },
+ ],
+ },
+ resolve: {
+ extensions: ['.ts', '.js'],
+ },
+};