File tree Expand file tree Collapse file tree 2 files changed +27
-6
lines changed Expand file tree Collapse file tree 2 files changed +27
-6
lines changed Original file line number Diff line number Diff line change 1
1
function getArticle ( url , api_key ) {
2
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
+
3
10
disableForm ( ) ;
4
11
5
12
const base_url = 'https://article-extractor2.p.rapidapi.com/article/parse?url=' ;
@@ -15,21 +22,27 @@ function getArticle(url, api_key) {
15
22
fetch ( base_url + url , options )
16
23
. then ( response => {
17
24
if ( response . status !== 200 ) {
18
- throw new Error ( `Request failed with status ${ response . status } ` ) ;
25
+ throw new Error ( `Request failed with status: ${ response . status } ` ) ;
19
26
}
20
27
return response . json ( ) ;
21
28
} )
22
29
. then ( response => {
30
+ if ( response . error >= 0 ) {
31
+ throw new Error ( `Request received error: ${ response . message } ` ) ;
32
+ }
33
+
23
34
console . log ( response ) ;
24
35
25
36
document . getElementById ( "quick-weblog-title" ) . value = response . data ?. title ;
26
37
document . getElementById ( "quick-weblog-image_url" ) . value = "Image via " + response . data ?. souce ;
27
38
document . getElementById ( "quick-weblog-image_description" ) . value = response . data ?. title ;
28
39
document . getElementById ( "quick-weblog-quote" ) . value = response . data ?. description ;
29
40
41
+ updateStatus ( "Successfully used Auto Fill to populate form!" ) ;
30
42
enableForm ( ) ;
31
43
} )
32
44
. catch ( err => {
45
+ updateStatus ( err ) ;
33
46
console . error ( err ) ;
34
47
enableForm ( ) ;
35
48
} ) ;
@@ -51,3 +64,7 @@ function enableForm() {
51
64
function disableForm ( ) {
52
65
toggleFormElements ( false ) ;
53
66
}
67
+
68
+ function updateStatus ( status ) {
69
+ document . getElementById ( "quick-weblog-status" ) . innerHTML = status ;
70
+ }
Original file line number Diff line number Diff line change @@ -52,21 +52,25 @@ function quick_weblog_form() {
52
52
53
53
<script>
54
54
window.addEventListener("DOMContentLoaded", (event) => {
55
- document.getElementById("quick-test").addEventListener("click", (click_event) => {
56
- getArticle(document.getElementById("quick-test-input").value, "<?php echo esc_js ( wp_kses ( $ api_key , array () ) ); ?> " );
55
+ document.getElementById("quick-weblog-auto").addEventListener("click", (click_event) => {
56
+ click_event.preventDefault();
57
+ getArticle(document.getElementById("quick-weblog-url").value, "<?php echo esc_js ( wp_kses ( $ api_key , array () ) ); ?> " );
57
58
});
58
59
});
59
60
</script>
60
61
61
62
<p id="quick-weblog-description">Quickly create a simple Post that highlights an existing news article. Posts include a captioned image and quote with URL citation of original article. All fields are required.</p>
62
63
63
- <button id="quick-test">Quick Test</button>
64
-
65
64
<div class="card">
66
65
<form id="quick-weblog" method="post" action="<?php echo esc_url ( admin_url ('admin-post.php ' ) ); ?> ">
67
66
<div>
68
67
<label for="url"><?php _e ( 'Post URL ' , 'quick-weblog ' ); ?> </label>
69
- <input id="quick-test-input" type="text" name="url" id="quick-weblog-url" required>
68
+ <input type="text" name="url" id="quick-weblog-url" required>
69
+ </div>
70
+
71
+ <div>
72
+ <button id="quick-weblog-auto">Auto Fill</button>
73
+ <span id="quick-weblog-status">Attempt to auto fill form based on <strong>Post URL</strong>.</span>
70
74
</div>
71
75
72
76
<div>
You can’t perform that action at this time.
0 commit comments