Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

download interceptor base #226

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,10 @@
}
}
},
"Extension_list": {
"message": "Intercept download of files with those extensions (space/colon separated)",
"description": "Header for settings section."
},
"Debugging_Output": {
"message": "Debugging Output",
"description": "Header for settings section."
Expand Down
2 changes: 2 additions & 0 deletions src/background/backgroundState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface BackgroundState {
notificationInterval: number | undefined;
showNonErrorNotifications: boolean;
isInitializingExtension: boolean;
interceptExtensions: string;
}

const state: BackgroundState = {
Expand All @@ -21,6 +22,7 @@ const state: BackgroundState = {
notificationInterval: undefined,
showNonErrorNotifications: true,
isInitializingExtension: true,
interceptExtensions: '',
};

export function getMutableStateSingleton() {
Expand Down
30 changes: 30 additions & 0 deletions src/background/downloadInterceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getMutableStateSingleton } from "./backgroundState";
import { addDownloadTasksAndPoll } from "./actions";

export function initializeDownloadInterceptor() {
downloads.onCreated.addListener((itm)=>{

const state = getMutableStateSingleton();
var ext, ext_list;

ext_list = state.interceptExtensions.split(' ');

// has no extension
if (itm.filename.index('.') < 0) return;

ext = itm.filename.split('.').pop().toLowerCase();

// extension not in the list
if (ext_list.indexOf(ext) < 0) return;

// stop download and add NAS task
downloads.cancel(itm.id);
addDownloadTasksAndPoll(
state.api,
state.pollRequestManager,
state.showNonErrorNotifications,
[itm.url],
);

});
}
2 changes: 2 additions & 0 deletions src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { saveLastSevereError } from "../common/errorHandlers";
import { onStoredStateChange as onStoredStateChangeListener } from "./onStateChange";
import { initializeContextMenus } from "./contextMenus";
import { initializeMessageHandler } from "./messages";
import { initializeDownloadInterceptor } from "./downloadInterceptor";

initializeContextMenus();
initializeMessageHandler();
initializeDownloadInterceptor();

maybeMigrateState()
.then(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/background/onStateChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,7 @@ export function onStoredStateChange(storedState: State) {
backgroundState.finishedTaskIds = new Set(updatedFinishedTaskIds);
}

backgroundState.interceptExtensions = storedState.settings.interceptExtensions;

backgroundState.isInitializingExtension = false;
}
1 change: 1 addition & 0 deletions src/common/state/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const SETTING_NAMES = typesafeUnionMembers<keyof Settings>({
shouldHandleDownloadLinks: true,
badgeDisplayType: true,
showInactiveTasks: true,
interceptExtensions: true,
});

const ALL_STORED_STATE_NAMES = typesafeUnionMembers<keyof State>({
Expand Down
23 changes: 23 additions & 0 deletions src/settings/SettingsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ export class SettingsForm extends React.PureComponent<Props, State> {
DOWNLOAD_ONLY_PROTOCOLS.join(", "),
])}
/>

<span className="label">{browser.i18n.getMessage("Extension_list")}</span>
<div className="input">
<input
type="text"
value={this.props.extensionState.settings.interceptExtensions}
onChange={(e) => {
var interceptExtensions = e.currentTarget.value
.replace(/[\;\,\.]/g,' ')// unify various separators into spaces
.toLowerCase()
.split(' ')
.filter((s)=>{ return s; })
.join(' ');
this.saveExtList(interceptExtensions);
}}
/>
</div>
</SettingsList>

{this.maybeRenderDebuggingOutputAndSeparator()}
Expand Down Expand Up @@ -265,6 +282,12 @@ export class SettingsForm extends React.PureComponent<Props, State> {
});
}

private saveExtList(interceptExtensions: string) {
this.saveSettings({
interceptExtensions,
});
}

private saveSettings = async (settings: Partial<Settings>) => {
const success = await this.props.saveSettings({
...typesafePick(this.props.extensionState.settings, ...SETTING_NAMES),
Expand Down