Skip to content

Instantly share code, notes, and snippets.

@kir
Created July 28, 2014 20:18
Show Gist options
  • Select an option

  • Save kir/844684e9d2c51372a778 to your computer and use it in GitHub Desktop.

Select an option

Save kir/844684e9d2c51372a778 to your computer and use it in GitHub Desktop.
Checkvist bookmarklet
maxkir_screen = function() {
var x,y;
if (self.innerHeight) {// all except Explorer
x = self.innerWidth;
y = self.innerHeight;
}
else if (document.documentElement && document.documentElement.clientHeight) {
// Explorer 6 Strict Mode
x = document.documentElement.clientWidth;
y = document.documentElement.clientHeight;
}
else {
x = document.body.clientWidth;
y = document.body.clientHeight;
}
return [x, y];
};
maxkir_trim = function(s) {
return s.replace(/(^\s+)|(\s+$)/g, "");
};
function maxkir_invoke() {
var host = window._cvHost;
if (!host) {
host = 'checkvist.com';
}
var proto = (document.location.protocol == 'https:' ? 'https:' : 'http:');
if (host.indexOf('localhost') != -1) {
proto = 'http:';
}
var f = proto + '//' +host+ '/checklists/?add_popup=1&d=' + encodeURIComponent(new Date().getTime());
var gmail_check = function(title) {
var id_data = /([0-9a-f]{10,})$/.exec(document.location.href);
if (id_data) {
var gmail_re = /^(?:Google Mail|Gmail) - (.*) - .*@.*$/i;
var title_data = gmail_re.exec(title);
if (title_data) {
return [id_data[1], title_data[1]];
}
}
return null;
};
var gmail_check_new = function(title) {
var id_data = /([0-9a-f]{10,})$/.exec(document.location.href);
if (id_data) {
var gmail_re = /^(.*) - .*@.* - (?:Google Mail|Gmail)$/i;
var title_data = gmail_re.exec(title);
if (title_data) {
return [id_data[1], title_data[1]];
}
}
return null;
};
var _tracker_check = function(title_regexp, title) {
var id_data = /([\w-]+)(\?.*)?$/.exec(document.location.href.split(/#/)[0]);
if (id_data) {
var id = id_data[1];
// ID should contain '-'
if (!/-/.test(id)) return null;
var title_data = new RegExp(title_regexp.replace(/#ID#/, id)).exec(title);
if (title_data) {
return [id, title_data[1]];
}
}
return null;
};
var jira_check = function(title) {
return _tracker_check("^\\[##ID#\\]:?\\s*(.*) - .*$", title);
};
var youtrack_check = function(title) {
var res = _tracker_check("^\\[?#ID#\\]?:?\\s*(.*)$", title);
if (res) return res;
return _tracker_check("^(.*)\\s*: #ID#$", title);
};
var link_check = function(title) {
return [document.location.href, title];
};
var empty = function(selection) {
return !selection || selection.toString() == '' || selection.toString() == 'null';
};
var get_sel = function() {
var selection = '';
if (window.getSelection) {
selection = window.getSelection();
} else if (document.getSelection) {
selection = document.getSelection();
} else if (document.selection) {
selection = document.selection.createRange().text;
}
if (empty(selection)) {
// Get selection from iFrames
var frames = document.getElementsByTagName('iframe');
if (frames && frames.length) {
for(var j = 0; j < frames.length; j++) {
var iframe = frames[j];
var idoc = null;
try { idoc = iframe.document; } catch (e) {}
if (!idoc) {
try { idoc = iframe.contentWindow ? iframe.contentWindow.document : null; } catch (e) {}
}
if (!idoc) {
try { idoc = iframe.contentDocument; } catch (e) {}
}
try {
if (idoc && idoc.getSelection) {
selection = '' + idoc.getSelection();
}
}
catch(e) {
// Cannot get selection from iFrame
}
if (!empty(selection)) break;
}
}
}
return empty(selection) ? '' :selection;
};
var s = function () {
var selection = get_sel();
var title_elements = document.getElementsByTagName('TITLE');
var title = title_elements && title_elements.length > 0 ? title_elements[0].innerHTML : '';
title = maxkir_trim(title).replace(/(\n|\r)+/g, ' ');
if (!title) {
title = document.location.href;
}
var procs = [
{fn: gmail_check, pattern: "[gmail: ##TITLE##|##ID##]"},
{fn: gmail_check_new, pattern: "[gmail: ##TITLE##|##ID##]"},
{fn: jira_check, pattern: "[jira: ##ID##|" +document.location.href+"] ##TITLE##"},
{fn: youtrack_check, pattern: "[youtrack: ##ID##|" +document.location.href+"] ##TITLE##"},
{fn: link_check, pattern: "[link: ##TITLE##|##ID##]"}
];
var txt = '';
for (var i = 0; i < procs.length; i ++) {
var data = procs[i]["fn"](title);
if (data) {
txt = procs[i]["pattern"].replace("##ID##", data[0]).replace("##TITLE##", data[1]);
break;
}
}
return [txt, selection];
};
var a = function() {
var data = s();
var url = f +
'&import_cotent=' + encodeURIComponent(data[0]) +
'&import_cotent_note=' + encodeURIComponent(data[1]) +
"&from_url=" + encodeURIComponent(top.location.href);
var iframe = document.createElement("iframe");
iframe.src = url;
iframe.width = 530;
iframe.style.width = "530px";
iframe.height = 345;
iframe.style.height = "345px";
iframe.style.border = "4px solid #ccc";
var container = document.createElement("div");
container.appendChild(iframe);
container.style.position = "fixed";
container.style.left = (maxkir_screen()[0] - 530 - 8) + "px";
container.style.top = "-4px";
container.style.width = "530";
container.style.zIndex = 2199999999;
document.body.appendChild(container);
var getScroll = function() { return [window.scrollX, window.scrollY]; };
var setScroll = function(scroll) { window.scrollTo(scroll[0], scroll[1]); };
var setLocation = function(l) {
try {
location.replace(l);
}
catch(e) {
location.href = l;
}
};
var oldScroll;
var close_monitor = setInterval(function() {
var hash = location.href.split('#')[1];
if (/^CVST/.test(hash)) {
clearInterval(close_monitor);
setLocation(location.href.split('#')[0] + "#");
container.parentNode.removeChild(container);
if (oldScroll) {
setScroll(oldScroll);
setTimeout(function() { setScroll(oldScroll); }, 10);
}
}
else {
oldScroll = getScroll();
}
}, 50);
};
a();
};
maxkir_invoke();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment