Skip to content

Commit

Permalink
Update fetchURLTitle() to remove jquery, & remove html script tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ymeynot45 committed Jun 30, 2022
1 parent 32a38cf commit 769e136
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 39 deletions.
56 changes: 26 additions & 30 deletions app/assets/javascripts/application.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -118,35 +118,6 @@ var _Lobsters = Class.extend({
}
});
},

fetchURLTitle: function(button, url_field, title_field) {
if (url_field.val() == "")
return;

var old_value = button.val();
button.prop("disabled", true);
button.val("Fetching...");

$.post("/stories/fetch_url_attributes", {
fetch_url: url_field.val(),
})
.success(function(data) {
if (data) {
if (data.title)
title_field.val(data.title.substr(0, title_field.maxLength));
if (data.url)
url_field.val(data.url);
}

button.val(old_value);
button.prop("disabled", false);
})
.error(function() {
button.val(old_value);
button.prop("disabled", false);
});
Lobster.checkStoryTitle();
},
});

var Lobsters = new _Lobsters();
Expand Down Expand Up @@ -347,8 +318,29 @@ class _LobstersFunction {
}
}

fetchURLTitle(button, urlField, titleField) {
fetchURLTitle(button) {
const targetUrl = document.getElementById('story_url').value;
const title_field = document.getElementById('story_title');
const formData = new FormData();
const old_text = button.textContent;
button.setAttribute("disabled", true);
button.textContent = "Fetching...";
formData.append('fetch_url', targetUrl);

if (targetUrl == "")
return;

fetchWithCSRF('/stories/fetch_url_attributes', {
method: 'post',
headers: new Headers({'X-Requested-With': 'XMLHttpRequest'}),
body: formData,})
.then (response => response.json())
.then (data => {
title_field.value = data.title
button.textContent = old_text
});
button.removeAttribute("disabled");
Lobster.checkStoryTitle();
}

flagComment(voterEl) {
Expand Down Expand Up @@ -567,6 +559,10 @@ onPageLoad(() => {

Lobster.checkStoryTitle()

on('click', '#story_fetch_title', (event) => {
Lobster.fetchURLTitle(event.target);
});

on('click', 'li.story a.upvoter', (event) => {
event.preventDefault();
Lobster.upvoteStory(event.target);
Expand Down
9 changes: 0 additions & 9 deletions app/views/stories/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,4 @@ See the guidelines below for more." %>
:class => "normal" %>
</div>
</div>

<script>
$(document).ready(function() {
$("#story_fetch_title").click(function() {
Lobsters.fetchURLTitle($(this), $("#story_url"), $("#story_title"));
return false;
});
});
</script>
<% end %>

0 comments on commit 769e136

Please sign in to comment.