This repository was archived by the owner on Mar 16, 2020. It is now read-only.
forked from coltonrusch/precursor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowser.js
More file actions
158 lines (132 loc) · 3.92 KB
/
Copy pathBrowser.js
File metadata and controls
158 lines (132 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import React, {Component} from "react";
import {
StyleSheet,
Text,
View,
TextInput,
Keyboard,
Image,
TouchableHighlight,
ActivityIndicator
} from "react-native";
import {WebView} from "react-native-webview";
import HTMLView from 'react-native-htmlview';
import * as constants from "./CONSTANTS"
class Browser extends Component {
constructor(props) {
super(props);
this.state = {
editedHTML: "",
editedHTMLURL: "",
currentURLFiltered: false,
warningThreshold: 30,
numberOfReplacements: 0,
};
let wordList = []
}
render() {
return (
<View style={styles.root}>
<View style={styles.browserTitleContainer}>
<Text style={styles.browserTitle}>
PreCursor Browser
</Text>
</View>
{this.displayPopUp()}
<WebView
originWhitelist={['*']}
source={{html: this.state.editedHTML}}
onLoad={this.onBrowserLoad}
/>
</View>
);
}
//#####################################################################
// FILTERING METHODS
//#####################################################################
// called when the webview is first loaded
componentDidMount(){
let url = this.props.navigation.state.params.url;
wordList = this.props.navigation.state.params.words;
let replacer = "■■■■■■■■"
this.getHTML(url)
.then(html => this.replaceText(html, url, replacer))
.catch((err) => console.error("Error with filtering:" + err));
};
getHTML = async (url) => {
const response = await fetch(url); // fetch page
return await response.text(); // get response text
}
replaceText = (html, url, replacer) => {
wordList.forEach(element => {
if (element !== undefined)
html = this.replaceAllNoCase(html, element, replacer)
});
let replacements = ( html.match(new RegExp(replacer, "g")) || [] ).length;
this.setState((prevState) => ({
currentURLFiltered: true,
editedHTMLURL: url,
editedHTML: html,
numberOfReplacements : replacements}))
}
replaceAllNoCase = (word, strReplace, strWith) => {
// See http://stackoverflow.com/a/3561711/556609
var esc = strReplace.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
var reg = new RegExp(esc, 'ig');
if (word == null) return "";
else return word.replace(reg, strWith);
};
displayPopUp = () => {
if (this.state.warningThreshold < this.state.numberOfReplacements){
return <View style={styles.browserTitleContainer}>
<Text style={styles.browserTitle}>
This page may be uncomfortable for you
</Text>
</View>
}
}
}
const styles = StyleSheet.create({
browser: {
flex: 1
},
root: {
flex: 1,
backgroundColor: 'skyblue'
},
icon: {
width: 30,
height: 30,
resizeMode: 'contain'
},
disabled: {
opacity: 0.3
},
browserTitleContainer: {
height: 30,
justifyContent: 'center',
paddingLeft: 8
},
browserTitle: {
fontWeight: 'bold'
},
browserBar: {
height: 40,
backgroundColor: 'steelblue',
flexDirection: 'row',
alignItems: 'center'
},
browserAddressBar: {
height: 40,
backgroundColor: 'white',
borderRadius: 3,
flex: 1,
borderWidth: 0,
marginRight: 8,
paddingLeft: 8
},
browserContainer: {
flex: 2
}
});
export default Browser;