-
Notifications
You must be signed in to change notification settings - Fork 887
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Custom Scriptlet section in the settings page.
- Loading branch information
Showing
24 changed files
with
891 additions
and
25 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
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
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
51 changes: 51 additions & 0 deletions
51
...ources/settings/default_brave_shields_page/components/brave_adblock_scriptlet_editor.html
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,51 @@ | ||
<style include="settings-shared md-select"> | ||
div[slot=body] { | ||
overflow: hidden; | ||
} | ||
|
||
.flex { | ||
display: grid; | ||
grid-template-columns: 1fr 70%; | ||
align-items: baseline; | ||
} | ||
|
||
.content { | ||
margin-bottom: 5px; | ||
} | ||
|
||
textarea { | ||
white-space: pre; | ||
overflow-wrap: normal; | ||
overflow-x: scroll; | ||
width: 100%; | ||
min-height: 350px; | ||
max-height: 700px; | ||
resize: vertical; | ||
box-sizing: border-box; | ||
} | ||
</style> | ||
|
||
<cr-dialog id="dialog" show-close-button show-on-attach> | ||
<div slot="title">[[dialogTitle_]]</div> | ||
<div slot="body"> | ||
<div class="flex"> | ||
<div>$i18n{adblockCustomSciptletDialogNameLabel}</div> | ||
<cr-input id="scriptlet-name" value="{{scriptlet.name}}" invalid="[[!isScriptletValid_]]" autofocus required | ||
spellcheck="false" on-input="validateName_" error-message="[[scriptletErrorMessage_]]"> | ||
</cr-input> | ||
</div> | ||
<div> | ||
<div class="flex content">$i18n{adblockCustomScriptletDialogContentLabel}</div> | ||
<textarea id="scriptlet-content" multiline="true" value="{{scriptlet.content::input}}" spellcheck="false"> | ||
</textarea> | ||
</div> | ||
</div> | ||
<div slot="button-container"> | ||
<cr-button class="cancel-button" id="cancel" | ||
on-click="cancelClicked_">$i18n{adblockCustomScriptletDialogCancelButton} | ||
</cr-button> | ||
<cr-button class="action-button" id="save" on-click="saveClicked_" disabled="[[!isScriptletValid_]]"> | ||
$i18n{adblockCustomScriptletDialogSaveButton} | ||
</cr-button> | ||
</div> | ||
</cr-dialog> |
126 changes: 126 additions & 0 deletions
126
...esources/settings/default_brave_shields_page/components/brave_adblock_scriptlet_editor.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,126 @@ | ||
// Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
import 'chrome://resources/cr_elements/cr_button/cr_button.js' | ||
import { CrDialogElement } from 'chrome://resources/cr_elements/cr_dialog/cr_dialog.js' | ||
import 'chrome://resources/cr_elements/cr_input/cr_input.js' | ||
|
||
import { PrefsMixin } from '/shared/settings/prefs/prefs_mixin.js' | ||
import { I18nMixin } from 'chrome://resources/cr_elements/i18n_mixin.js' | ||
import { PolymerElement } from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js' | ||
|
||
import { getTemplate } from './brave_adblock_scriptlet_editor.html.js' | ||
|
||
import { | ||
Scriptlet, | ||
BraveAdblockBrowserProxyImpl, | ||
ErrorCode | ||
} from '../brave_adblock_browser_proxy.js' | ||
|
||
interface AdblockScriptletEditor { | ||
$: { | ||
dialog: CrDialogElement | ||
} | ||
} | ||
|
||
const AdblockScriptletEditorBase = I18nMixin(PrefsMixin(PolymerElement)) | ||
|
||
class AdblockScriptletEditor extends AdblockScriptletEditorBase { | ||
static get is() { | ||
return 'adblock-scriptlet-editor' | ||
} | ||
|
||
static get template() { | ||
return getTemplate() | ||
} | ||
|
||
static get properties() { | ||
return { | ||
scriptlet: Scriptlet, | ||
dialogTitle_: String, | ||
isScriptletValid_: Boolean, | ||
scriptletErrorMessage_: String | ||
} | ||
} | ||
|
||
scriptlet: Scriptlet | ||
dialogTitle_: string | ||
isScriptletValid_: boolean | ||
scriptletErrorMessage_: string | ||
|
||
scriptletName_: string | ||
browserProxy_ = BraveAdblockBrowserProxyImpl.getInstance() | ||
|
||
override ready() { | ||
super.ready() | ||
this.scriptletName_ = this.scriptlet.name | ||
|
||
if (this.scriptletName_) { | ||
this.dialogTitle_ = this.i18n('adblockEditCustomScriptletDialogTitle') | ||
} else { | ||
this.dialogTitle_ = this.i18n('adblockAddCustomScriptletDialogTitle') | ||
} | ||
|
||
this.updateError(ErrorCode.kOK) | ||
} | ||
|
||
updateError(error_code: ErrorCode) { | ||
this.isScriptletValid_ = error_code === ErrorCode.kOK | ||
switch (error_code) { | ||
case ErrorCode.kOK: | ||
this.scriptletErrorMessage_ = '' | ||
break | ||
case ErrorCode.kAlreadyExists: | ||
this.scriptletErrorMessage_ = this.i18n( | ||
'adblockEditCustomScriptletAlreadyExistsError' | ||
) | ||
break | ||
case ErrorCode.kInvalidName: | ||
this.scriptletErrorMessage_ = this.i18n( | ||
'adblockEditCustomScriptletInvalidNameError' | ||
) | ||
break | ||
case ErrorCode.kNotFound: | ||
this.scriptletErrorMessage_ = this.i18n( | ||
'adblockEditCustomScriptletNotFoundError' | ||
) | ||
break | ||
} | ||
} | ||
|
||
cancelClicked_() { | ||
this.$.dialog.cancel() | ||
} | ||
|
||
saveClicked_() { | ||
if (this.scriptletName_) { | ||
this.browserProxy_ | ||
.updateCustomScriptlet(this.scriptletName_, this.scriptlet) | ||
.then((e) => { | ||
this.updateError(e) | ||
if (this.isScriptletValid_) { | ||
this.$.dialog.close() | ||
} | ||
}) | ||
} else { | ||
this.browserProxy_.addCustomScriptlet(this.scriptlet).then((e) => { | ||
this.updateError(e) | ||
if (this.isScriptletValid_) { | ||
this.$.dialog.close() | ||
} | ||
}) | ||
} | ||
} | ||
|
||
validateName_() { | ||
if (!/^[a-z-_.]*$/.test(this.scriptlet.name)) { | ||
this.updateError(ErrorCode.kInvalidName) | ||
} else { | ||
this.updateError(ErrorCode.kOK) | ||
} | ||
} | ||
} | ||
|
||
customElements.define(AdblockScriptletEditor.is, AdblockScriptletEditor) |
Oops, something went wrong.