Skip to content

Instantly share code, notes, and snippets.

View VictorZhang2014's full-sized avatar

Victor VictorZhang2014

View GitHub Profile
@VictorZhang2014
VictorZhang2014 / background.js
Created May 16, 2023 17:08 — forked from danharper/background.js
Bare minimum Chrome extension to inject a JS file into the given page when you click on the browser action icon. The script then inserts a new div into the DOM.
// this is the background code...
// listen for our browerAction to be clicked
chrome.browserAction.onClicked.addListener(function (tab) {
// for the current tab, inject the "inject.js" file & execute it
chrome.tabs.executeScript(tab.ib, {
file: 'inject.js'
});
});
@VictorZhang2014
VictorZhang2014 / WyvernExchange.sol
Created November 17, 2022 15:44
opensea v1.0 contract WyvernExchange.sol
/**
*Submitted for verification at Etherscan.io on 2018-06-12
*/
pragma solidity ^0.4.13;
library SafeMath {
/**
* @dev Multiplies two numbers, throws on overflow.
@VictorZhang2014
VictorZhang2014 / INSColorPickerView.swift
Created December 15, 2020 14:20
It's a color picker from any UIView, Instagram-like color picker. - Swift 5.2
import UIKit
private var INSColorPickerWindow: UIWindow?
class INSColorPickerView: UIViewController {
private var screenshotForCollectionViewAsImage: UIImage?
private var completion: ((UIColor) -> Void)?
private let penView = UIView()
@VictorZhang2014
VictorZhang2014 / UIView+Extension.swift
Created December 15, 2020 14:14
Get color from point, you specify a CGPoint, the function returns that point as a UIColor to you. - Swift 5.2
extension UIView {
func getColourFromPoint(point:CGPoint) -> UIColor {
let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
var pixelData:[UInt8] = [0, 0, 0, 0]
let context = CGContext(data: &pixelData, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
context?.translateBy(x: -point.x, y: -point.y)
if let _context = context {
@VictorZhang2014
VictorZhang2014 / UICollectionView+Extension.swift
Created December 15, 2020 14:10
Get a snapshot or screenshot of visible UICollectionViewCell in UICollectionView - Swift 5.2
extension UICollectionView {
// Snapshot of UICollectionView, wherever you scroll, it captures the visible cells on screen as an image
func snapshot() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(self.frame.size, false, 0)
UIGraphicsBeginImageContext(bounds.size)
self.drawHierarchy(in: frame, afterScreenUpdates: true)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return screenshot
$wget https://github.com/google/protobuf/releases/download/v2.5.0/protobuf-2.5.0.tar.bz2
$tar xvf protobuf-2.5.0.tar.bz2
$cd protobuf-2.5.0
$./configure CC=clang CXX=clang++ CXXFLAGS='-std=c++11 -stdlib=libc++ -O3 -g' LDFLAGS='-stdlib=libc++' LIBS="-lc++ -lc++abi"
$make -j 4
$sudo make install
$protoc --version
@VictorZhang2014
VictorZhang2014 / WebSocketChat.js
Last active December 15, 2020 14:12
Javascript WebSocke chat example, it supports mobile and PC pages communication.
/**
* Doc: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
* */
'use strict';
// 通信协议定义
class YLGroupChatProtocol {
constructor () {
this.create_room = 1001;
this.send_content = 1002;
@VictorZhang2014
VictorZhang2014 / NotificationMessages.js
Created October 24, 2019 12:04
The simplest notification, Subscribe/Publish in Javascript
// Notification Messages
// This is made by Publish-Subscribe Topics
class YLNotificationMessages {
constructor () {
// Event Objects: storing event name and event callback
this.events = {};
}
// Subscribe Event
import tornado.ioloop
import tornado.web
import os
from tornado.options import define, options
define("port", default=8100, help="run on the given port", type=int)
class MainHandler(tornado.web.RequestHandler):
def get(self):
@VictorZhang2014
VictorZhang2014 / MonitorEvent.cs
Created April 30, 2019 09:28
C# Monitor WMI Events
// https://stackoverflow.com/questions/21731044/is-there-a-way-to-attach-an-event-handler-to-the-list-of-running-processes-in-c
//
static void Main(string[] args)
{
var query = new EventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance isa \"Win32_Process\"");
using (var eventWatcher = new ManagementEventWatcher(query))
{
eventWatcher.EventArrived += eventWatcher_EventArrived;
eventWatcher.Start();