Skip to content

Instantly share code, notes, and snippets.

@aruseni
Created January 10, 2014 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aruseni/8351752 to your computer and use it in GitHub Desktop.
Save aruseni/8351752 to your computer and use it in GitHub Desktop.
Searches for Redmine messages in the inbox and returns the links to the tasks. Use with the HTML version of GMail: https://mail.google.com/?ui=html‎
// gmail-redmine-task-list.js
//
// Searches for Redmine messages in the inbox
// and returns the links to the tasks
//
// Use with the HTML version of GMail:
// https://mail.google.com/?ui=html‎
var query = "redmine in:inbox";
var redmine_url = "https://redmine.example.com/"
function loadJS(src, callback) {
var s = document.createElement('script');
s.src = src;
s.async = true;
s.onreadystatechange = s.onload = function() {
var state = s.readyState;
if (!callback.done && (!state || /loaded|complete/.test(state))) {
callback.done = true;
callback();
}
};
document.getElementsByTagName('head')[0].appendChild(s);
}
function get_redmine_tasks() {
var tasks = [];
var offset_increment = 20;
var task_id_re = /#(\d+)/;
function process_page(offset) {
console.log("Loading… (offset=" + offset + ")");
// Load search results for the given query
jQuery.get(
window.location.pathname,
{"s": "q", "q": query, "st": offset}
).done(function(data) {
var html = jQuery(data);
// Look for Redmine task IDs in the subjects of the messages
html.find("span.ts b").each(function (index, val) {
var matches = val.innerHTML.match(task_id_re);
if (matches && matches.length == 2 && jQuery.inArray(matches[1], tasks) == -1) {
// Add the task’s ID if the subject matches the expected
// format and the task’s ID has not been added to the
// list already.
tasks.push(matches[1]);
}
});
offset += offset_increment;
if (html.find("a[href$='st=" + offset + "']").length == 2) {
// If there are links to the next page, continue processing
process_page(offset);
} else {
// Otherwise print the list of task IDs
console.log("All results loaded.")
jQuery.each(tasks, function(index, val) {
console.log(redmine_url + "issues/" + val);
});
}
});
}
// Process the first page
process_page(0);
}
loadJS("https://code.jquery.com/jquery-1.10.2.min.js", get_redmine_tasks);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment