diff --git a/README.md b/README.md index dea2913..3aeb84b 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Android port of OMORI RPG game - [ ] Options menu fix - [x] Physical gamepad support -- [ ] replaceAll polyfill +- [x] replaceAll polyfill - [ ] External saves (Android/data) - [ ] Cordova localhost bug - [x] WakeLock diff --git a/www/js/porting/compat/nwjs.js b/www/js/porting/compat/nwjs.js index d5851cc..ef761bd 100644 --- a/www/js/porting/compat/nwjs.js +++ b/www/js/porting/compat/nwjs.js @@ -2,6 +2,26 @@ window._MEMO_PATHS = {} +/** + * String.prototype.replaceAll() polyfill + * https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/ + * @author Chris Ferdinandi + * @license MIT + */ +if (!String.prototype.replaceAll) { + String.prototype.replaceAll = function(str, newStr){ + + // If a regex pattern + if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') { + return this.replace(str, newStr); + } + + // If a string + return this.replace(new RegExp(str, 'g'), newStr); + + }; +} + function replaceSpecialSymbols(s) { const memoized = _MEMO_PATHS[s]; if (memoized != undefined) {