Browse Source

add to transmet button

csrf
Dan Ballard 7 years ago
parent
commit
b45c66c94e
  1. 3
      config/local.json
  2. 1
      main.go
  3. 44
      route_handlers.go
  4. 1
      templates/layout.html
  5. 22
      templates/pages/list.html

3
config/local.json

@ -5,5 +5,6 @@
"Username": "transmet",
"Password": "asdfasdf"
},
"Port": "8001"
"Port": "8001",
"Url": "http://localhost:8001/"
}

1
main.go

@ -23,6 +23,7 @@ type Config struct {
Password string
}
Port string
Url string
}
const (

44
route_handlers.go

@ -114,6 +114,25 @@ func LogoutHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
http.Redirect(w, r, "/", http.StatusFound)
}
func getUrlTitle(url string) string {
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error looking up link", url, ":", err)
} else {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading link", url, ":", err)
} else {
re := regexp.MustCompile("< *[Tt][Ii][Tt][Ll][Ee] *>(.*)</ *[Tt][Ii][Tt][Ll][Ee] *>")
title := re.FindStringSubmatch(string(body))
if title != nil {
return strings.TrimSpace(title[1])
}
}
}
return ""
}
// ?url=
func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
categories.LoadCategories(db)
@ -136,28 +155,17 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
}
var url = r.URL.Query().Get("url")
reHttp := regexp.MustCompile("^http://")
reHttp := regexp.MustCompile("^https?://")
if url != "" && ! reHttp.Match([]byte(url)) {
url = "http://" + url
}
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error looking up link", url, ":", err)
} else {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading link", url, ":", err)
} else {
re := regexp.MustCompile("< *[Tt][Ii][Tt][Ll][Ee] *>(.*)</ *[Tt][Ii][Tt][Ll][Ee] *>")
title := re.FindStringSubmatch(string(body))
if title != nil {
ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": strings.TrimSpace(title[1])})
return
}
}
title = r.URL.Query().Get("title")
if title == "" && url != "" {
title = getUrlTitle(url)
}
ShowTemplate("add", w, map[string]interface{}{"user": user, "link": url, "categories": categories.CategoriesTree, "flashes": flashes})
ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": title})
}
func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
@ -208,7 +216,7 @@ func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User
fmt.Println("Exec err: ", err)
}
ShowTemplate("list", w, map[string]interface{}{"user": user, "flashes": flashes, "template": &templateBuf})
ShowTemplate("list", w, map[string]interface{}{"user": user, "flashes": flashes, "template": &templateBuf, "url": config.Url})
}
func templatePostHandler(w http.ResponseWriter, r *http.Request, user *user.User) {

1
templates/layout.html

@ -30,6 +30,7 @@
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="/add">add</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
{{if .user}}

22
templates/pages/list.html

@ -4,12 +4,32 @@
<form class="form-add" action="/" method="post" role="form" class="container col-form">
<div class="row">
<div class="col-xs-10"></div><div class="col-xs-2">
<div class="col-xs-2">
<a class="btn btn-lg btn-primary btn-block" href="{{template "launch-add" .}}">Add to transmet</a>
</div>
<div class="col-xs-4"></div>
<div class="col-xs-4"></div>
<div class="col-xs-2">
<input class="btn btn-lg btn-primary btn-block" type="submit" value="Export" />
</div>
<div class="col-xs-6">
Drag this link to bookmark bar and click anywhere to add a link
</div>
<div class="col-xs-6">
Click this to mark the current queue of news items as exported (clearing them)
</div>
<div class="col-xs-12">&nbsp;</div>
</div>
</form>
<textarea class="form-control" name="template" placeholder="Template" rows="16" cols="80">{{.template}}</textarea>
{{end}}
{{define "launch-add"}}
javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,s=(e?e():(k)?k():(x?x.createRange().text:0)),f='{{.url}}add',l=d.location,e=encodeURIComponent,u=f+'?url='+e(l.href)+'&title='+e(d.title);a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570'))l.href=u;};if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();void(0)
{{end}}
Loading…
Cancel
Save