Skip to content

Instantly share code, notes, and snippets.

@xtravar
xtravar / Global Amazon Script.js
Created August 20, 2020 01:53
A Tampermonkey script that converts currencies and whatnot on Amazon sites. WIP.
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://*.amazon.de/*
// @match https://*.amazon.co.jp/*
// @match https://*.amazon.co.uk/*
// @match https://*.amazon.it/*
@xtravar
xtravar / HTMLUtilities.swift
Created August 29, 2018 04:53
HTML and JavaScript string escaping/manipulating functions
func makeJSCall(_ fnName: String, _ arguments: Any...) -> String {
let argString = arguments.map { escapeForJavaScript($0) }.joined(separator: ", ")
return "\(fnName)(\(argString))"
}
func escapeForJavaScript(_ value: Any) -> String {
let data = try! JSONSerialization.data(withJSONObject: [value], options: [])
let str = String(data: data, encoding: .utf8)!.dropFirst().dropLast()
return String(str)
@xtravar
xtravar / AttributedStringStruct.swift
Created August 3, 2018 00:18
NSAttributedString doing interpolation and being all struct-y
public struct AttributedString {
fileprivate var _handle: NSMutableAttributedString
fileprivate init(handle: NSMutableAttributedString) {
self._handle = handle
}
fileprivate init(_ str: NSAttributedString) {
self.init(handle: str.mutableCopy() as! NSMutableAttributedString)
@xtravar
xtravar / HashableWrapper.swift
Last active June 9, 2024 14:43
Use classes and objects as keys to dictionaries and sets.
/*
Swift AnyObject and AnyClass are not Hashable, so they cannot be used in Dictionaries as keys or Sets as members.
This code concisely shows how to make wrapper structs for these types and then convenience subscripts/methods to wrap/unwrap.
This is for the case where you wish to associate a reference, not object content, with a value or used in a set.
Example use case: a dependency injector that returns an object for a protocol.
*/
-- 1. Create a new generic password entry in Keychain Access called "WHATEVER_AnyConnect_VPN" (the name in Keychain access must match that in line 39 below) with your password for the Cisco AnyConnect VPN server.
-- 2. Open this script in Script Editor (both this and the above are in the Applications->Utilities folder) and "Save as.." an Application (.app) with desired name.
-- 3. Open Security & Privacy System Preferences, go to Privacy, Accessibility.
-- 4. Enable the above .app so it can access Accessibility
-- 5. Copy and paste a nice icon on the generic Applescript icon (I used a copy of the default AnyConnect one)
-- 6. Add the new .app to /Users/[yourshortname]/Applications with a shortcut to your Dock
-- 7. Enjoy the fast connection with no need to enter password and increased security of not having a sensitive password stored as plain text
-- 8. Run script again to close connection
---------------------
import Foundation
import UIKit
// emulates table view sticky headers and demonstrates content inset usage
public class FakeTableViewController: UIViewController, UIScrollViewDelegate {
// set up views neatly using anonymous closures
let scrollView: UIScrollView = {
let v = UIScrollView()
v.backgroundColor = UIColor.lightGray
return v
@xtravar
xtravar / Creatable.md
Last active February 29, 2016 16:33
Creatable Swift extension

The Problem

I really like setting properties in their declaration line. It just feels so good to leverage Swift's let.

The drawback is that you must then set properties' properties in init, somewhat splitting the "setup" into two stages and mixing it with the "usage".

    public class MyView : UIView {
        private let titleLabel = UILabel()
 
@xtravar
xtravar / YahooMailAutocompleteHide.js
Created January 10, 2016 19:11
Greasemonkey/Tampermonkey script for killing Yahoo Mail's autocomplete dialog
// ==UserScript==
// @name Stop Yahoo Mail Autocomplete
// @namespace http://xtravar.org
// @version 0.1
// @description Stops the stupid autocomplete from yahoo mail
// @author Xtravar
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @match *.mail.yahoo.com/*
// @grant none
// ==/UserScript==
//
// Punycode.swift
//
// Created by Mike Kasianowicz on 9/7/15.
// Copyright © 2015 Mike Kasianowicz. All rights reserved.
//
import Foundation
public class Punycode {