new strategy for surf mode

This commit is contained in:
Kyle Drake 2015-03-26 11:53:41 -07:00
parent 0efa3cada0
commit 75f6c5d31b
6 changed files with 47 additions and 7 deletions

6
app.rb
View file

@ -21,7 +21,13 @@ helpers do
end
end
set :protection, :frame_options => "ALLOW-FROM #{$config['surf_iframe_source']}"
before do
if request.host.match /\w+\.#{Addressable::URI.parse($config['surf_proxy_uri']).host}/i
surf_proxy
end
if request.path.match /^\/api\//i
@api = true
content_type :json

View file

@ -16,6 +16,28 @@ get '/surf/:username' do |username|
erb :'surf', layout: false
end
def surf_proxy
username = request.host.match /^(\w+)/i
resp = RestClient.get "http://#{username}.neocities.org#{request.path}"
content_type resp.headers[:content_type]
if content_type.match(/^text\/html/)
body = resp.body+erb(:'surf/_surf_iframe_injection', layout: false)
else
body = resp.body
end
halt body
end
def surf_proxy_uri(username)
$surf_proxy_uri ||= Addressable::URI.parse $config['surf_proxy_uri']
new_uri = $surf_proxy_uri.dup
new_uri.host = "#{username}.#{new_uri.host}"
new_uri.to_s
end
=begin
get %r{\/surf\/proxy\/([\w-]+)\/(.+)|\/surf\/proxy\/([\w-]+)\/?} do
captures = params[:captures].compact
username = captures.first
@ -39,8 +61,10 @@ get %r{\/surf\/proxy\/([\w-]+)\/(.+)|\/surf\/proxy\/([\w-]+)\/?} do
site_body.gsub(/(?<name>\b\w+\b)\s*=\s*(?<value>"[^"]*"|'[^']*'|[^"'<>\s]+)/i) do |ele|
attributes.each do |attr|
if ele.match attr
uri = ele.match(/\"(.+)\"|\'(.+)\'/).captures.first
if ele.match /#{attr} ?=/
ele_match = ele.match(/\"(.+)\"|\'(.+)\'/)
next if ele_match.nil? || ele_match.captures.nil?
uri = ele_match.captures.first
new_ele = nil
@ -61,3 +85,4 @@ get %r{\/surf\/proxy\/([\w-]+)\/(.+)|\/surf\/proxy\/([\w-]+)\/?} do
new_site_body
end
=end

View file

@ -63,13 +63,13 @@
<ul class="row website-Gallery content int-Gall">
<% @sites.each_with_index do |site,i| %>
<li>
<a href="<%= site.uri %>" class="neo-Screen-Shot" title="<%= site.title %>" onclick="return true; surf(<%= ((@current_page-1)*Site::BROWSE_PAGINATION_LENGTH)+i+1 %>); return false">
<a href="<%= site.uri %>" class="neo-Screen-Shot" title="<%= site.title %>" onclick="surf(<%= ((@current_page-1)*Site::BROWSE_PAGINATION_LENGTH)+i+1 %>); return false">
<span class="img-Holder" style="background:url(<%= site.screenshot_url('index.html', '540x405') %>) no-repeat;">
<img src="/img/placeholder.png" alt="<%= site.title %>" />
</span>
</a>
<div class="title">
<a href="<%= site.uri %>" title="<%= site.title %>" onclick="return true; surf(<%= i+1 %>); return false"><%= site.title.shorten(30) %></a>
<a href="<%= site.uri %>" title="<%= site.title %>" onclick="surf(<%= i+1 %>); return false"><%= site.title.shorten(30) %></a>
</div>
<div class="site-info">
<div class="username">

View file

@ -235,9 +235,8 @@
<a class="close" href="//<%= @site.host %>" alt="close" title="close"></a>
</li>
</ul>
</div>
<iframe src="<%= "#{$config['surf_proxy_uri']}/surf/proxy/#{@site.username}" %>" frameborder="0" scrolling="yes" marginheight="0" marginwidth="0" id="website-frame" name="website-frame"></iframe>
<iframe src="<%= "#{surf_proxy_uri @site.username}" %>" frameborder="0" scrolling="yes" marginheight="0" marginwidth="0" id="website-frame" name="website-frame"></iframe>
<script src="/js/jquery-1.11.0.min.js"></script>
<script src="/js/bootstrap.min.js"></script>

View file

@ -0,0 +1,10 @@
<script>
function _nc_surf_page_overload() {
var ele = document.getElementsByTagName('a')
for(var i=0; i<ele.length; i++) {
if(ele[i].href.match(/^\w+:\/\//i) && !ele[i].href.match(/^.+\.neocities\.org/i))
ele[i].setAttribute('target', '_blank')
}
}
_nc_surf_page_overload()
</script>