Initial commit

Change-Id: I1dab5335b68b9f095be6ff5774e7f6b1875796c8
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..2371531
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,10 @@
+node_modules
+temp
+.tmp
+dist
+.sass-cache
+app/bower_components
+test/bower_components
+package
+app/scripts
+.DS_Store
\ No newline at end of file
diff --git a/images/icon-16.png b/images/icon-16.png
new file mode 100644
index 0000000..a425dad
--- /dev/null
+++ b/images/icon-16.png
Binary files differ
diff --git a/images/icon-32.png b/images/icon-32.png
new file mode 100644
index 0000000..6482708
--- /dev/null
+++ b/images/icon-32.png
Binary files differ
diff --git a/manifest.json b/manifest.json
new file mode 100644
index 0000000..05a4dc7
--- /dev/null
+++ b/manifest.json
@@ -0,0 +1,19 @@
+{
+  "name": "Gerrit bug reporter",
+  "version": "0.0.1",
+  "manifest_version": 2,
+  "description": "Pre-popupate gerrit bug reports with screenshots",
+  "icons": {
+    "16": "images/icon-16.png",
+    "128": "images/icon-32.png"
+  },
+  "browser_action": {
+    "default_popup": "popup.html",
+    "default_title": "Click here!"
+  },
+  "permissions": [
+    "activeTab",
+    "<all_urls>",
+    "tabs"
+  ]
+}
diff --git a/popup.html b/popup.html
new file mode 100644
index 0000000..67f30fe
--- /dev/null
+++ b/popup.html
@@ -0,0 +1,38 @@
+<html>
+  <head>
+    <script src="popup.js"></script>
+  </head>
+  <body style="width: 400px">
+    <style>
+      body {
+        font-family: Arial, Helvetica, sans-serif;
+        font-size: 12px;
+      }
+      input,
+      textarea {
+        width: 100%;
+      }
+      #target {
+        width: 100%;
+      }
+      #imgContainer {
+        overflow: auto;
+      }
+      .hidden {
+        display: none;
+      }
+    </style>
+    <div id="error">
+      To use this plugin, go to a gerrit site.
+    </div>
+    <div id="gerritBugReport" class="hidden">
+      <p>Bug report title<input id="summary" type="text"></p>
+      <p>Description<textarea id="description"
+            placeholder="Include all repro steps and browser/OS if applicable"></textarea>
+      </p>
+      <button id="screenshotBtn">Add screenshot</button>
+      <button id="submitBtn">Pre-populate bug report</button>
+      <div id="imgContainer"><img id="target"/></div>
+    </div>
+  </body>
+</html>
\ No newline at end of file
diff --git a/popup.js b/popup.js
new file mode 100644
index 0000000..25d93f0
--- /dev/null
+++ b/popup.js
@@ -0,0 +1,75 @@
+function GerritBugReport(currentTab) {
+  this.image = '';
+  this.url = currentTab;
+};
+GerritBugReport.prototype._addEventListeners = function(button, submitBtn) {
+  button.addEventListener('click', this._handleTap.bind(this));
+  submitBtn.addEventListener('click', this._handleSubmit.bind(this));
+}
+GerritBugReport.prototype._handleTap = function() {
+  chrome.tabs.captureVisibleTab(null, {format: 'png'}, function(img) {
+    this.image = img;
+    document.getElementById('target').src = img;
+    document.getElementById('screenshotBtn').classList.add('hidden');
+  }.bind(this));
+};
+GerritBugReport.prototype._computeQueryString = function(url, queryParams) {
+  for (const key of Object.keys(queryParams)) {
+    url += `${key}=${queryParams[key]}&`
+  }
+  return url;
+};
+GerritBugReport.prototype._reset = function(url, queryParams) {
+  document.getElementById('summary').value = '';
+  document.getElementById('description').value = '';
+  this.image = '';
+  document.getElementById('target').src = '';
+  document.getElementById('submitBtn').disabled = false;
+};
+GerritBugReport.prototype._handleSubmit = function() {
+  const summary = document.getElementById('summary').value;
+  let description = 'Bug report from ' + encodeURIComponent(this.url) +
+      '%0A%0A' + document.getElementById('description').value;
+  const components = 'PolyGerrit';
+  const queryParams = { summary, description, components }
+  let url = 'https://bugs.chromium.org/p/gerrit/issues/entry?';
+  if (!this.image) {
+    url = this._computeQueryString(url, queryParams);
+    this._reset();
+    return chrome.tabs.create({url: url}, function(tab){ alert(tab.id) });
+  }
+
+  // Post image to imgur
+  document.getElementById('submitBtn').disabled = true;
+  var xhr = new XMLHttpRequest(), formData = new FormData();
+  xhr.open('POST', 'https://api.imgur.com/3/image.json', true);
+  formData.append('image', this.image.replace('data:image/png;base64,' , ''));
+  xhr.onreadystatechange = function() {
+    if (xhr.readyState == XMLHttpRequest.DONE) {
+      const imageLink = JSON.parse(xhr.responseText).data.link;
+      description = description + '%0A%0A'
+          + 'Screenshot: ' + imageLink;
+      queryParams.description = description;
+      url = this._computeQueryString(url, queryParams);
+      this._reset();
+      chrome.tabs.create({'url': url}, function(tab){ alert(tab.id) })
+    }
+  }.bind(this)
+  xhr.setRequestHeader('Authorization', 'Client-ID 2c8d8a8a1a0d4cb')
+  xhr.send(formData);
+};
+
+document.addEventListener('DOMContentLoaded', function() {
+  chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
+    const currentTab = tabs[0].url;
+    if (!currentTab.startsWith('https://gerrit-review.googlesource.com')) {
+      return;
+    }
+    document.getElementById('error').classList.add('hidden');
+    document.getElementById('gerritBugReport').classList.remove('hidden');
+    const button = document.getElementById('screenshotBtn');
+    const submitBtn = document.getElementById('submitBtn');
+    const gerritBugReport = new GerritBugReport(currentTab);
+    gerritBugReport._addEventListeners(button, submitBtn);
+  });
+});
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..e225a47
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,3 @@
+# Gerrit Bug Reporter - Chrome extension
+
+This chrome extension allows users to pre-populate a monorail bug report for gerrit-review.googlesource.com, Where a screenshot can be added (uploaded to imgur) for a seamless bug reporting experience
\ No newline at end of file