Skip to content

Instantly share code, notes, and snippets.

View mg's full-sized avatar

Magnús Örn Gylfason mg

View GitHub Profile
@mg
mg / bgglog.dart
Last active February 28, 2024 10:00
Log gameplay to BGG example
import 'dart:convert';
import 'package:http/http.dart' as http;
const BGGLOGIN_URL = 'https://boardgamegeek.com/login/api/v1';
const BGGUPLOAD_URL = 'https://boardgamegeek.com/geekplay.php';
enum UploadResult {
Success,
UsernamePassword,
NetworkError,
class MyListNotifier extends ValueNotifier<MyList> {
MyListNotifier(MyList myList) : super(myList) {
value.setNotifyListeners(notifyListeners);
}
}
class MyList<E> extends List<E> {
VoidCallback _notifyListeners;
void setNotifyListeners(VoidCallback notifyListeners) {
this.notifyListeners = notifyListeners;
class MyWidget extends StatefulWidget {
final GlobalKey myKey = GlobalKey();
MyWidget();
@override
State<StatefulWidget> createState() => MyWidgetState();
}
class MyWidgetState extends State<MyWidget> {
@override
@mg
mg / copy.js
Created July 25, 2016 11:20
Copy Amazon Kindle Highlights in Chrome
var items= document.getElementsByClassName("highlight")
var lines= []
for(var i= 0; i < items.length; i++) lines.push(items[i].innerText)
copy(lines.join("\n\n"))
items := make(map[string]interface{})
items["Sources"] = ghsources
items["SourcesIndex"] = sourceindex
items["UserName"] = u.String()
items["UserLogoutUrl"], _ = user.LogoutURL(c, "/")
if err = t.ExecuteTemplate(w, "admin.thtml", items); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
func NewSetFromSlice(s interface{}) Set {
a := NewSet()
slice := reflect.ValueOf(s)
for i := 0; i < slice.Len(); i++ {
a.Add(slice.Index(i).Interface())
}
return a
}
@mg
mg / lipm
Created November 27, 2013 12:44
(function($) {
window.LI = $.extend(window.LI || {}, {exec: function(m, a, s, e) {
jQuery.when(jQuery.ajax({type: 'POST',url: '/Services/MethodProxy.asmx/Execute',data: '{"methodName":"' + m + '","args":' + (a.toJSON ? a.toJSON() : JSON.stringify(a)) + '}',contentType: 'application/json;charset=ISO-8859-1',dataType: 'json')
.then(
function(r) { s(r.d); },
function(r) {
if (r.responseText == '')
return;
var x = JSON.parse(r.responseText);
if (x.Stacktrace) {
@mg
mg / powerset.pl
Last active December 24, 2015 04:59
The Devils code
#!/usr/bin/perl
sub sos_as_string ($;$) {
my ( $set, $string ) = @_;
$$string .= '{';
my $i;
foreach my $key ( keys %{ $set } ) {
$$string .= ' ' if $i++;
if ( ref $set->{ $key } ) {
@mg
mg / HOC-for-context-and-props.jsx
Created November 22, 2015 18:37
Describing a pattern I use to leverage the benefits context in React.js but still keeping my components simple
/*
This pattern is useful when using context in React to pass values down
the component tree. The example I use here is localization but I use it every
time I use contexts in React
*/
/*
Locale is a Higher Order Component that creates the "locale" context. Components
that are locale sensitive can use this context value to both get the active
locale and to be notified when the locale changes. It also puts the function
let promises= [...]
function wrap(inner) {
let outer= new Promise()
inner.then(result => {
outer.resolve(result)
}).catch(error) {
outer.resolve(error)
}
return outer