Skip to content

Commit 4099c1d

Browse files
committed
WIP - AutoFill JavaScript
- fetching successfully and logging article data - TODO: needs some serious cleanup, remove test IDs specifically
1 parent 31b29ff commit 4099c1d

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

quick-weblog.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
function getArticle(url, api_key) {
12

2-
const backup = 'https://article-extractor2.p.rapidapi.com/article/parse?url=https%3A%2F%2Frapidapi.com%2Fblog%2Frapidapi-marketplace-is-now-rapidapi-hub%2F';
3+
const base_url = 'https://article-extractor2.p.rapidapi.com/article/parse?url=';
34

4-
function getArticle(url) {
55
const options = {
66
method: 'GET',
77
headers: {
@@ -10,13 +10,17 @@ function getArticle(url) {
1010
}
1111
};
1212

13-
fetch(url, options)
14-
.then(response => response.json())
15-
.then(response => console.log(response))
16-
.catch(err => console.error(err));
13+
fetch(base_url + url, options)
14+
.then(response => {
15+
if (response.status !== 200) {
16+
throw new Error(`Request failed with status ${response.status}`);
17+
}
18+
return response.json();
19+
})
20+
.then(response => {
21+
console.log(response);
22+
})
23+
.catch(err => {
24+
console.error(err);
25+
});
1726
}
18-
19-
console.log('quick-weblog.js success');
20-
21-
22-
{/* <script type="module" src="<?php echo plugin_dir_url( __FILE__ ); ?>quick-weblog.js"></script> */}

quick-weblog.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
Text Domain: quick-weblog
1313
*/
1414

15-
$api_key = get_option('quick_weblog_api_key', '');
16-
1715
function quick_weblog_form() {
16+
17+
$api_key = get_option('quick_weblog_api_key', '');
18+
1819
?>
1920
<style>
2021
#quick-weblog {
@@ -47,13 +48,25 @@ function quick_weblog_form() {
4748
}
4849
</style>
4950

51+
<script src="<?php echo plugin_dir_url( __FILE__ ); ?>quick-weblog.js"></script>
52+
53+
<script>
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() ) ); ?>" );
57+
});
58+
});
59+
</script>
60+
5061
<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>
5162

63+
<button id="quick-test">Quick Test</button>
64+
5265
<div class="card">
5366
<form id="quick-weblog" method="post" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>">
5467
<div>
5568
<label for="url"><?php _e( 'Post URL', 'quick-weblog' ); ?></label>
56-
<input type="text" name="url" id="url" required>
69+
<input id="quick-test-input" type="text" name="url" id="url" required>
5770
</div>
5871

5972
<div>

0 commit comments

Comments
 (0)