-
Notifications
You must be signed in to change notification settings - Fork 21
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
#214 feature/implement replace js function #351
#214 feature/implement replace js function #351
Conversation
…from the options page
…arbitrary JS execution
…arbitrary JS execution
…tyx0/foxreplace into feature/implement_replace_js_function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, thanks for the pull request and sorry for not responding before.
First of all, congratulation because it is a great work and you clearly know JavaScript better than me. And also thanks for translating the new strings to Catalan. I suppose you have used automatic translation but it is still fairly good.
I have added some comments with things that should be improved and other things.
options/gridcomponents.js
Outdated
InputTypeEditor.getOption(params, 1, browser.i18n.getMessage("inputType.wholeWords")) + | ||
InputTypeEditor.getOption(params, 2, browser.i18n.getMessage("inputType.regExp")) + | ||
this.options.reduce((accum, curOption, i) => | ||
DropdownEditor.getOption(params, i, browser.i18n.getMessage(curOption)) + accum |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dropdown elements appear in reverse order from specified in code. The concatenation should be accum + DropdownEditor.getOption(...)
instead.
options/options.html
Outdated
@@ -218,6 +218,7 @@ <h4 class="i18n">options.otherSettings</h4> | |||
</div> | |||
</div> | |||
<div id="substitutionsContent" class="tab-pane" role="tabpanel"> | |||
<p class="i18n">options.output.function.warning</p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a graphical warning sign would be more outstanding. For example:
<p><i class="fas fa-exclamation-triangle"></i> <span class="i18n">options.output.function.warning</span></p>
scripts/core.js
Outdated
this.caseSensitive = !!caseSensitive; | ||
this.outputType = +outputType; | ||
this.inputType = +inputType; | ||
|
||
if (this.inputType < this.INPUT_TEXT || this.inputType > this.INPUT_REG_EXP) this.inputType = this.INPUT_TEXT; // avoid invalid values |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you could add another line like this for the output type, to ensure it is a valid value.
scripts/core.js
Outdated
|
||
case this.INPUT_WHOLE_WORDS: | ||
// necessary according to https://stackoverflow.com/q/1520800 | ||
this.regExp.lastIndex = 0; | ||
return string.replace(this.regExp, (word, index, string) => { | ||
// todo by someone who understands: clarify what the following block does and why it does it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember the details because I wrote it many years ago, but it has to do with correct functioning of replace whole words with non-ASCII characters, including respecting the special strings $$
, $&
, etc. It was added in df6fe58.
scripts/core.js
Outdated
|
||
} | ||
|
||
replaceWithFn(userReplaceInput, curStrToReplace) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add some documentation to this method, like in the others. Maybe some of the comments below could be moved to this method documentation.
scripts/core.js
Outdated
let inputType = this.prototype.INPUT_TYPE_STRINGS.indexOf(json.inputType); | ||
return new Substitution(json.input, json.output, json.caseSensitive, inputType); | ||
const inputType = this.prototype.INPUT_TYPE_STRINGS.indexOf(json.inputType); | ||
const outputType = this.prototype.OUTPUT_TYPE_STRINGS.indexOf(json.outputType); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you should check if the JSON really has an outputType
, otherwise it fails. Look how the mode is filled in SubstitutionGroup.fromJSON
for reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have to update the version in substitutionListToJSON
. Use version 2.6, which will be the version of FoxReplace when this is released. Also, in checkVersion
set 2.6 as the current version and add 2.1 to the list of old versions.
scripts/replace.js
Outdated
@@ -47,6 +47,7 @@ function replaceWindow(aWindow, aSubstitutionList, aPrefs) { | |||
case group.HTML_NONE: replaceText(doc, consecutiveGroups, aPrefs); break; | |||
case group.HTML_OUTPUT: replaceTextWithHtml(doc, consecutiveGroups, aPrefs); break; | |||
case group.HTML_INPUT_OUTPUT: replaceHtml(doc, consecutiveGroups, aPrefs); break; | |||
default: console.error('Fox Replace: Invalid output type'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is necessary, but in any case it is FoxReplace, without space, and it would be the HTML mode that is wrong, not the output type. But since there are no other checks like this in this file, I would remove the line entirely.
@@ -24,7 +24,7 @@ | |||
<link rel="stylesheet" href="../lib/bootstrap.css"> | |||
<script src="../lib/xregexp-all.js"></script> | |||
<script src="../scripts/core.js"></script> | |||
<script src="sidebar.js"></script> | |||
<script defer src="./sidebar.js"></script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of this defer
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To ensure sidebar.js only loads once sidebar.html fully loads. If sidebar.js runs before sidebar.html fully renders, dom selectors will fail
<option value="0" selected="" class="i18n">outputType.text</option> | ||
<option value="1" class="i18n">outputType.function</option> | ||
</select> | ||
</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could add the warning also here, below the output type, preferably shown only if "function" is selected.
icon to preferences page)
…outputType undefined
@Woundorf thanks for your feedback! addressed all comments |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only one issue with the DropdownEditor. Everything else is fine.
options/gridcomponents.js
Outdated
@@ -139,7 +146,7 @@ class DropdownEditor { | |||
init(params) { | |||
this.gui = $('<select>' + | |||
this.options.reduce((accum, curOption, i) => | |||
DropdownEditor.getOption(params, i, browser.i18n.getMessage(curOption)) + accum | |||
accum + DropdownEditor.getOption(params, i, browser.i18n.getMessage(curOption)) + accum |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have added the accum to the beginning but not removed it from the end. Now some items are repeated multiple times.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ugh good catch. hit c-z
too many times. fixed
Summary
Feature implemented: Users can replace using JS functions. The provided input will run as the
return
statement tostring.replace
and can use the same variables mentioned in MDNExample:
dino(saur)
Regular Expression
"great big " + match + " biga" + p1.toUpperCase()
Function
great big dinosaur bigaSAUR
Screenshot
UI changes:
select
in the sidebar and in the options page featuringoptions
"text" and "function"This PR addresses issue #214