Skip to content

Instantly share code, notes, and snippets.

@olegp
olegp / background.js
Created September 20, 2018 20:31
Active tab change detection in Chrome Extension
let activeTabId, lastUrl, lastTitle;
function getTabInfo(tabId) {
chrome.tabs.get(tabId, function(tab) {
if(lastUrl != tab.url || lastTitle != tab.title)
console.log(lastUrl = tab.url, lastTitle = tab.title);
});
}
chrome.tabs.onActivated.addListener(function(activeInfo) {
@olegp
olegp / raffle.js
Last active March 20, 2018 17:43
((p) => p[Math.floor(Math.random() * (p.length))])(`
Alberto Marchetti
Daniel Schildt
Ekaterina Dorrer
Fernando Girón
Iines Piesala
Joonas Haaparanta
Majedul Hoque Ruman
Margarita Obraztsova
Matti Vakkilainen
<div>
<form ng-submit="submit()">
<input ng-model="text">
<button>Submit</button>
</form>
<ul>
<li ng-repeat="message in messages">{{message.time}} {{message.text}}</li>
</ul>
</div>
@olegp
olegp / placeholder.js
Created December 30, 2015 12:52
input placeholder shim
if (!('placeholder' in document.createElement('input'))) {
angular.module('directives').directive('placeholder', ['$timeout', function($timeout) {
return function($scope, $element, $attrs) {
var hasFocus = false;
$element.bind('focus', function() {
hasFocus = true;
if ($element.hasClass('placeholder')) {
$timeout(function() {
$element.val('');
$element.removeClass('placeholder');
@olegp
olegp / prettify.js
Last active December 29, 2015 10:26
JS code for generating SEO friendly URL fragments
function prettify(text) {
return text.toLowerCase()
.replace(/ä/g, 'a')
.replace(/ö/g, 'o')
.replace(/å/g, 'a')
.replace(/[^0-9a-z]+/g, '-')
.replace(/^-*(.*?)-*$/, '$1');
}
@olegp
olegp / test.js
Created December 15, 2014 20:32
test
module.exports = "test";
var express = require('express');
var app = express();
var messages = [
{ date: new Date(), text: "Hello World" }
];
app.use('/', express.static('./'));
app.get('/api/', function(req, res){
@olegp
olegp / similarity.js
Created March 4, 2012 20:35
String Similarity
// JS port of http://www.catalysoft.com/articles/StrikeAMatch.html
function similarity(str1, str2) {
function letterPairs(str) {
var pairs = [];
for ( var i = 0; i < str.length - 1; i++) {
pairs.push(str.substr(i, i + 2));
}
return pairs;
}
@olegp
olegp / analyzer.js
Created February 25, 2012 10:50
Time Combined Log Analyzer
var stream = require('fs-base').open(system.args[2]);
var stats = {}, paths = [], line;
while(line = stream.readLine()) {
try {
var l = line.substr(line.indexOf('"'));
l = l.split(/"([^"]+)"/);
var url = l[1].split(' ')[1].substr(1).split('/');
url = url.concat(url.pop().split('?'));
var time = +l[6].trim();
@olegp
olegp / gist:1652459
Created January 21, 2012 11:36
Disable logging in Ringo
var logLevels = {
"org.hibernate": "WARN",
"com.mchange": "ERROR",
"net.sf.ehcache": "ERROR"
};
var {Logger, Level} = org.apache.log4j;
for(var p in logLevels) {
Logger.getLogger(p).setLevel(Level[logLevels[p]]);
}