Load plugins JS resources upon initial page rendering

Bug: Issue 3915
Change-Id: Ic2d1d96458ecb2cd09dcd1cd204f8a06f234dbb7
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html
index 2fede7c..1a6413f 100644
--- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html
+++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html
@@ -35,7 +35,8 @@
 
     setup(function() {
       element = fixture('basic');
-      Gerrit.install(function(p) { plugin = p; });
+      Gerrit.install(function(p) { plugin = p; },
+          'http://test.com/plugins/testplugin/static/test.js');
     });
 
     teardown(function() {
@@ -43,6 +44,12 @@
       plugin = null;
     });
 
+    test('url', function() {
+      assert.equal(plugin.url(), 'http://test.com/plugins/testplugin/');
+      assert.equal(plugin.url('/static/test.js'),
+          'http://test.com/plugins/testplugin/static/test.js');
+    });
+
     test('history event', function(done) {
       plugin.on(element.EventType.HISTORY, function(path) {
         assert.equal(path, '/path/to/awesomesauce');
@@ -56,8 +63,8 @@
       var testChange = {
         _number: 42,
         revisions: {
-          def: { _number: 2 },
-          abc: { _number: 1 },
+          def: {_number: 2},
+          abc: {_number: 1},
         },
       };
       plugin.on(element.EventType.SHOW_CHANGE, function(change, revision) {
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
index b5c84d3..a9d81e3f 100644
--- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
+++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
@@ -14,13 +14,27 @@
 (function(window) {
   'use strict';
 
-  function Plugin() {}
+  function Plugin(opt_url) {
+    this._url = new URL(opt_url);
+    if (this._url.pathname.indexOf('/plugins') !== 0) {
+      console.warn('Plugin not being loaded from /plugins base path:',
+          this._url.href, '— Unable to determine name.');
+      return;
+    }
+    this._name = this._url.pathname.split('/')[2];
+  }
+
+  Plugin.prototype._name = '';
 
   Plugin.prototype.on = function(eventName, callback) {
     document.createElement('gr-js-api-interface').addEventCallback(eventName,
         callback);
   };
 
+  Plugin.prototype.url = function(opt_path) {
+    return this._url.origin + '/plugins/' + this._name + (opt_path || '/');
+  };
+
   var Gerrit = window.Gerrit || {};
 
   Gerrit.css = function(rulesStr) {
@@ -35,8 +49,10 @@
     return name;
   },
 
-  Gerrit.install = function(callback) {
-    callback(new Plugin());
+  Gerrit.install = function(callback, opt_src) {
+    // TODO(andybons): Polyfill currentScript for IE10/11 (edge supports it).
+    var src = opt_src || (document.currentScript && document.currentScript.src);
+    callback(new Plugin(src));
   };
 
   window.Gerrit = Gerrit;