hack to get the <title> tag when importing a url, without CORS issues
This commit is contained in:
parent
cc986368be
commit
cc2e3b9358
4 changed files with 56 additions and 0 deletions
35
app/controllers/hacks_controller.rb
Normal file
35
app/controllers/hacks_controller.rb
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
# bad code that should be seriously checked over before entering one of the
|
||||||
|
# other prim and proper files in the nice section of this repo
|
||||||
|
class HacksController < ApplicationController
|
||||||
|
include ActionView::Helpers::TextHelper # string truncate method
|
||||||
|
|
||||||
|
def load_url_title
|
||||||
|
authorize :Hack
|
||||||
|
url = params[:url] # TODO verify!?!?!?!
|
||||||
|
response, url = get_with_redirects(url)
|
||||||
|
title = get_encoded_title(response)
|
||||||
|
render json: { success: true, title: title, url: url }
|
||||||
|
rescue StandardError => e
|
||||||
|
render json: { success: false, error_type: e.class.name, error_message: e.message }
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def get_with_redirects(url)
|
||||||
|
uri = URI.parse(url)
|
||||||
|
response = Net::HTTP.get_response(uri)
|
||||||
|
while response.code == '301'
|
||||||
|
uri = URI.parse(response['location'])
|
||||||
|
response = Net::HTTP.get_response(uri)
|
||||||
|
end
|
||||||
|
[response, uri.to_s]
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_encoded_title(http_response)
|
||||||
|
title = http_response.body.sub(/.*<title>(.*)<\/title>.*/m, '\1')
|
||||||
|
charset = http_response['content-type'].sub(/.*charset=(.*);?.*/, '\1')
|
||||||
|
charset = nil if charset == 'text/html'
|
||||||
|
title = title.force_encoding(charset) if charset
|
||||||
|
truncate(title, length: 140)
|
||||||
|
end
|
||||||
|
end
|
5
app/policies/hack_policy.rb
Normal file
5
app/policies/hack_policy.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class HackPolicy < ApplicationPolicy
|
||||||
|
def load_url_title?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
|
@ -80,4 +80,8 @@ Metamaps::Application.routes.draw do
|
||||||
get 'users/:id/details', to: 'users#details', as: :details
|
get 'users/:id/details', to: 'users#details', as: :details
|
||||||
post 'user/updatemetacodes', to: 'users#updatemetacodes', as: :updatemetacodes
|
post 'user/updatemetacodes', to: 'users#updatemetacodes', as: :updatemetacodes
|
||||||
resources :users, except: [:index, :destroy]
|
resources :users, except: [:index, :destroy]
|
||||||
|
|
||||||
|
namespace :hacks do
|
||||||
|
get 'load_url_title'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -88,6 +88,18 @@ const PasteInput = {
|
||||||
import_id,
|
import_id,
|
||||||
{
|
{
|
||||||
success: function(topic) {
|
success: function(topic) {
|
||||||
|
$.get('/hacks/load_url_title', {
|
||||||
|
url: text
|
||||||
|
}, function success(data, textStatus) {
|
||||||
|
var selector = '#showcard #topic_' + topic.get('id') + ' .best_in_place'
|
||||||
|
if ($(selector).find('form').length > 0) {
|
||||||
|
$(selector).find('textarea, input').val(data.title)
|
||||||
|
} else {
|
||||||
|
$(selector).html(data.title)
|
||||||
|
}
|
||||||
|
topic.set('name', data.title)
|
||||||
|
topic.save()
|
||||||
|
})
|
||||||
TopicCard.showCard(topic.get('node'), function() {
|
TopicCard.showCard(topic.get('node'), function() {
|
||||||
$('#showcard #titleActivator').click()
|
$('#showcard #titleActivator').click()
|
||||||
.find('textarea, input').focus()
|
.find('textarea, input').focus()
|
||||||
|
|
Loading…
Reference in a new issue