1
- function getArticle ( url , api_key ) {
2
-
3
- if ( url === "" ) {
4
- updateStatus ( "You must fill out <strong>Post URL</strong> to use Auto Fill." ) ;
5
- return ;
6
- } else {
7
- updateStatus ( "Attempting to fetch information and Auto Fill..." ) ;
8
- }
9
-
10
- disableForm ( ) ;
11
-
1
+ function fetchArticleData ( url , api_key ) {
12
2
const base_url = 'https://article-extractor2.p.rapidapi.com/article/parse?url=' ;
13
-
14
3
const options = {
15
4
method : 'GET' ,
16
5
headers : {
@@ -19,37 +8,55 @@ function getArticle(url, api_key) {
19
8
}
20
9
} ;
21
10
22
- fetch ( base_url + url , options )
23
- . then ( response => {
24
- if ( response . status !== 200 ) {
25
- throw new Error ( `Request failed with status: ${ response . status } ` ) ;
26
- }
27
- return response . json ( ) ;
28
- } )
29
- . then ( response => {
30
- console . log ( response ) ;
11
+ return fetch ( base_url + url , options )
12
+ . then ( response => {
13
+ if ( response . status !== 200 ) {
14
+ throw new Error ( `Request failed with status: ${ response . status } ` ) ;
15
+ }
16
+ return response . json ( ) ;
17
+ } )
18
+ . then ( response => {
19
+ if ( response . error > 0 ) {
20
+ throw new Error ( `Request received error: ${ response . message } ` ) ;
21
+ }
22
+ return response . data ;
23
+ } )
24
+ . catch ( err => {
25
+ throw new Error ( `Error fetching article data: ${ err . message } ` ) ;
26
+ } ) ;
27
+ }
31
28
32
- if ( response . error > 0 ) {
33
- throw new Error ( `Request received error: ${ response . message } ` ) ;
34
- }
29
+ function updateFormFields ( data ) {
30
+ document . getElementById ( "quick-weblog-title" ) . value = data ?. title ;
31
+ document . getElementById ( "quick-weblog-image_url" ) . value = data ?. image ;
32
+ document . getElementById ( "quick-weblog-image_description" ) . value = "image via " + data ?. source ;
33
+ document . getElementById ( "quick-weblog-quote" ) . value = data ?. description ;
34
+ }
35
+
36
+ function fetchAndPopulateFormFields ( articleUrl , rapidApiKey ) {
37
+ if ( articleUrl === "" ) {
38
+ updateStatus ( "You must fill out <strong>Post URL</strong> to use Auto Fill." ) ;
39
+ return ;
40
+ }
35
41
36
- document . getElementById ( "quick-weblog-title" ) . value = response . data ?. title ;
37
- document . getElementById ( "quick-weblog-image_url" ) . value = response . data ?. image ;
38
- document . getElementById ( "quick-weblog-image_description" ) . value = "image via " + response . data ?. source ;
39
- document . getElementById ( "quick-weblog-quote" ) . value = response . data ?. description ;
42
+ updateStatus ( "Attempting to fetch information and Auto Fill..." ) ;
43
+ disableForm ( ) ;
40
44
41
- updateStatus ( "Successfully used Auto Fill to populate form!" ) ;
42
- enableForm ( ) ;
43
- } )
44
- . catch ( err => {
45
- updateStatus ( err ) ;
46
- console . error ( err ) ;
47
- enableForm ( ) ;
48
- } ) ;
45
+ fetchArticleData ( articleUrl , rapidApiKey )
46
+ . then ( data => {
47
+ updateFormFields ( data ) ;
48
+ updateStatus ( "Successfully used Auto Fill to populate form!" ) ;
49
+ enableForm ( ) ;
50
+ } )
51
+ . catch ( err => {
52
+ updateStatus ( err ) ;
53
+ console . error ( err ) ;
54
+ enableForm ( ) ;
55
+ } ) ;
49
56
}
50
57
51
58
function toggleFormElements ( enable ) {
52
- const form = document . getElementById ( "quick-weblog" ) ;
59
+ const form = document . getElementById ( "quick-weblog" ) ;
53
60
const formElements = form . querySelectorAll ( "input, select, textarea" ) ;
54
61
55
62
formElements . forEach ( element => {
0 commit comments