Example #1
0
func Init(wg *sync.WaitGroup) {
	wg.Add(1)
	go func() {
		defer wg.Done()
		js.Global.Call("addEventListener", "message", func(e *js.Object) {
			data := e.Get("data").String()
			var msg Message
			if err := json.Unmarshal([]byte(data), &msg); err != nil {
				log.Printf("Error decoding message from iframe: %s", err)
				return
			}
			go func() {
				data, err := fetchFile(&msg)
				if err != nil {
					log.Printf("Error fetching file: %s\n", err)
					return
				}
				if err := sendResponse(&msg, data); err != nil {
					log.Printf("Error sending response to iframe: %s\n", err)
					return
				}
			}()
		})
	}()
}
Example #2
0
// BeforeTransition prepares the page to study
func BeforeTransition(event *jquery.Event, ui *js.Object, p url.Values) bool {
	u, err := repo.CurrentUser()
	if err != nil {
		log.Printf("No user logged in: %s\n", err)
		return false
	}
	log.Debugf("card = %s\n", p.Get("card"))
	go func() {
		container := jQuery(":mobile-pagecontainer")
		// Ensure the indexes are created before trying to use them
		u.DB()

		card, err := repo.GetRandomCard()
		if err != nil {
			log.Printf("Error fetching card: %+v\n", err)
			return
		}
		log.Debugf("card = %s\n", card)
		body, iframeID, err := card.Body()
		if err != nil {
			log.Printf("Error parsing body: %+v\n", err)
			return
		}
		log.Debugf("body = %s\niframe = %s\n", body, iframeID)

		iframe := js.Global.Get("document").Call("createElement", "iframe")
		iframe.Call("setAttribute", "sandbox", "allow-scripts")
		iframe.Call("setAttribute", "seamless", nil)
		iframe.Set("id", iframeID)
		iframe.Set("src", "data:text/html;charset=utf-8;base64,"+base64.StdEncoding.EncodeToString([]byte(body)))

		js.Global.Get("document").Call("getElementById", "cardframe").Call("appendChild", iframe)

		jQuery(".show-until-load", container).Hide()
		jQuery(".hide-until-load", container).Show()
	}()

	return true
}
Example #3
0
func (c *Card) inlineSrc(n *html.Node) error {
	doc := goquery.NewDocumentFromNode(n)
	doc.Find("img").Each(func(i int, s *goquery.Selection) {
		src, ok := s.Attr("src")
		if !ok {
			log.Print("Found an image with no source!!??")
			return
		}
		log.Debugf("Found image with src of '%s'", src)
		att, err := c.GetAttachment(src)
		if err != nil {
			log.Printf("Error inlining file '%s': %s", src, err)
			return
		}
		s.SetAttr("src", fmt.Sprintf("data:%s;base64,%s", att.ContentType, base64.StdEncoding.EncodeToString(att.Content)))
		// iframe.Set("src", "data:text/html;charset=utf-8;base64,"+base64.StdEncoding.EncodeToString([]byte(body)))
	})
	return nil
}