--- /dev/null
+// TV 2 Developer Plugin
+// Javascript Implementation
+// @author Kristian Kræmmer Nielsen <jkkn@tv2.dk>
+
+var TV2Developer = {
+
+ _lastAction: null,
+ init: function() {
+ // init
+ this._lastAction = this.getPref('lastaction-linktype');
+ if (this.getPref('firstRun')) {
+ this.setPref('firstRun', false);
+ // add icon
+ try {
+ var firefoxnav = document.getElementById('nav-bar');
+ var curSet = firefoxnav.currentSet;
+ if (curSet.indexOf('tv2-swap-button') == -1)
+ {
+ var set;
+ // Place the button after the urlbar
+ if (curSet.indexOf('urlbar-container') != -1)
+ set = curSet.replace(/urlbar-container/, 'urlbar-container,tv2-swap-button');
+ else // otherwise at the end
+ set = firefoxnav.currentSet + ',tv2-swap-button';
+ firefoxnav.setAttribute('currentset', set);
+ firefoxnav.currentSet = set;
+ document.persist('nav-bar', 'currentset');
+ // If you don't do the following call, funny things happen
+ try {
+ BrowserToolboxCustomizeDone(true);
+ }
+ catch (e) { }
+ }
+ }
+ catch(e) { }
+ }
+ },
+
+ _strs: null,
+ get strs() {
+ if (!this._strs) {
+
+ this._strs = document.getElementById('tv2developer_strings');
+ }
+ return this._strs;
+ },
+
+ _prefs: null,
+ get prefs() {
+ if (!this._prefs)
+ this._prefs =
+ Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch('tv2developer.');
+ return this._prefs;
+ },
+
+ getPref: function(name, defValue) {
+ const CI=Components.interfaces.nsIPrefBranch;
+ const prefs=this.prefs;
+ try {
+ switch(prefs.getPrefType(name)) {
+ case CI.PREF_STRING:
+ return prefs.getCharPref(name);
+ case CI.PREF_INT:
+ return prefs.getIntPref(name);
+ case CI.PREF_BOOL:
+ return prefs.getBoolPref(name);
+ }
+ } catch(e) {alert(e)}
+ return defValue;
+ },
+
+ setPref: function(name, value) {
+ const prefs=this.prefs;
+ switch(typeof(value)) {
+ case 'string':
+ prefs.setCharPref(name, value);
+ break;
+ case 'boolean':
+ prefs.setBoolPref(name, value);
+ break;
+ case 'number':
+ prefs.setIntPref(name, value);
+ break;
+ default:
+ throw new Error('Unsupported type '+typeof(value)+' for preference '+name);
+ }
+ },
+
+ emptyMenu: function(menu) {
+ var children = menu.childNodes;
+ for (var i = children.length - 1; i >= 0; --i)
+ {
+ var index = children[i].getAttribute('tv2link');
+ if (index)
+ menu.removeChild(children[i]);
+ }
+ },
+
+ TV2Link: function(type, domain, uri, flags) {
+ this.type = type;
+ this.label = domain;
+ this.url = 'http://' + domain + uri;
+ this.disabled = false;
+ this.flags = (flags ? flags : '');
+ },
+
+ TV2LinkSplit: function () {
+ this.type = 'split';
+ },
+
+ TV2LinkWithLabel: function(type, label, url, flags, disabled) {
+ this.type = type;
+ this.label = label;
+ this.url = url;
+ this.disabled = typeof(disabled)!='undefined' ? disabled : false;
+ this.flags = (flags ? flags : '');
+ },
+
+ addMenuLink: function(menu, tv2link, defSet) {
+ var def = false;
+ var item;
+ if (tv2link.type == 'split') {
+ item = document.createElement('menuseparator');
+ } else {
+ item = document.createElement('menuitem');
+ item.setAttribute('label', tv2link.label);
+ item.setAttribute('url', tv2link.url);
+ item.setAttribute('tooltiptext', tv2link.label);
+ item.setAttribute('disabled', tv2link.disabled);
+ item.setAttribute('tv2flags', tv2link.flags);
+ if (!defSet && tv2link.type == this._lastAction) {
+ item.setAttribute('style', 'font-weight: bold');
+ def = true;
+ }
+ }
+ item.setAttribute('tv2link', tv2link.type);
+ menu.appendChild(item);
+ return def;
+ },
+
+ _reg_tv2: /^https?:\/\/([^/.]*)?([^/]*?)(\.(robot|opdatering2?|template)\.?)?(\.((test|snapshot)3?)\.)?tv2\.dk((\/[^#\?]*).*)?$/,
+ _reg_php: /^(.*\.php)/,
+ _reg_nodeid: /([0-9]{2,})/g,
+ _reg_i2files: /\/([0-9]+)-/,
+ //_reg_php5: /^(www|i2|common|ttvpumpe|1234|nyhederne)$/,
+ _reg_php5: /^(1234|i2|nyhederne)$/,
+ _reg_fromviewcvs: /^http:\/\/viewcvs\.tv2.dk:7467\/cgi-bin\/viewvc\.cgi\/([^/]+)\.tv2\.dk\/(webroot|robot|opdatering|template)([^#\?]+)/,
+ _reg_fromi2if: /^\/(tango\/(entry|requeue|dynamic)|tool\/pdo_log\/frameset)\.php\?url=([^&]+)/,
+ getLinks: function() {
+ var links = new Array();
+ var currentURL = getBrowser().currentURI.spec;
+ var encodedURL = encodeURIComponent(currentURL);
+ var initials = this.getPref('developer-initials');
+
+ // Adding links as appropiate if tv2.dk site
+ var tv2 = this._reg_tv2.exec(currentURL);
+ if (tv2) {
+ var skip_treenodes = false;
+ var tv2_sitename = tv2[1];
+ var tv2_user = tv2[2];
+ var tv2_sitetype = tv2[4];
+ var tv2_testsite = tv2[6];
+ var tv2_testtype = tv2[7]; /* no 3 */
+ var tv2_uri = tv2[8];
+ var tv2_uriOnly = tv2[9]; // no parameters, anchers
+
+ // Strip -dyn and -static
+ if (tv2_sitename) tv2_sitename = tv2_sitename.replace(/-(dyn|static)$/, '');
+ if (tv2_user) tv2_user = tv2_user.replace(/^\./, '');
+
+ // php5 sites
+ //var php5 = ((!tv2_sitename) || (this._reg_php5.test(tv2_sitename)));
+ var php5 = ((tv2_sitename) && (this._reg_php5.test(tv2_sitename)));
+ var testType = php5 ? '' : '3';
+ if (tv2_sitetype && tv2_sitetype == 'opdatering2') tv2_sitetype = 'opdatering';
+
+ // Find live, test and snapshot url for current site
+ var liveurl;
+ var live_sitename = (tv2_sitename=='www') ? '' : tv2_sitename+'.';
+ var cvs_sitename = tv2_sitename ? tv2_sitename : 'www';
+
+ if (tv2_testsite) { // test or snapshot
+ if (tv2_sitetype) { /* opdatering, ... */
+ liveurl = tv2_sitename+'.'+tv2_sitetype+'.tv2.dk';
+ } else {
+ liveurl = live_sitename+'tv2.dk';
+ }
+ }
+
+ var testprompturl = cvs_sitename+'.'+'?'+(tv2_sitetype?'.'+tv2_sitetype:'')+'.test'+testType+'.tv2.dk';
+ var testurl = cvs_sitename+'.'+initials+(tv2_sitetype?'.'+tv2_sitetype:'')+'.test'+testType+'.tv2.dk';
+ var snapshoturl = cvs_sitename+(tv2_sitetype?'.'+tv2_sitetype:'')+'.snapshot'+testType+'.tv2.dk';
+
+ // Add the two urls we are not at
+ if (!tv2_testsite) { // always add swaps to snapshot and the test site
+ links.push(new this.TV2Link('test_live_swap', testurl, tv2_uri));
+ links.push(new this.TV2Link('snapshot_live_swap', snapshoturl, tv2_uri));
+ links.push(new this.TV2Link('testprompt_swap', testprompturl, tv2_uri, 'promptInitials'));
+ } else if (tv2_testtype == 'snapshot') {
+ links.push(new this.TV2Link('snapshot_live_swap', liveurl, tv2_uri));
+ links.push(new this.TV2Link('test_snapshot_swap', testurl, tv2_uri));
+ links.push(new this.TV2Link('testprompt_swap', testprompturl, tv2_uri, 'promptInitials'));
+ } else { // must be test
+ links.push(new this.TV2Link('test_live_swap', liveurl, tv2_uri));
+ links.push(new this.TV2Link('test_snapshot_swap', snapshoturl, tv2_uri));
+ if (tv2_user != initials) {
+ links.push(new this.TV2Link('testprompt_swap', testurl, tv2_uri));
+ } else {
+ links.push(new this.TV2Link('testprompt_swap', testprompturl, tv2_uri, 'promptInitials'));
+ }
+ }
+
+ links.push(new this.TV2LinkSplit());
+
+ // Add link to .opdatering, .template, .robot
+ var _prefix = cvs_sitename + (tv2_user ? '.' + tv2_user : '');
+ var _postfix = (tv2_testsite ? '.' + tv2_testsite : '') + '.tv2.dk';
+ var opdatering = _prefix + '.opdatering' + _postfix;
+ //var template = _prefix + '.template' + _postfix;
+ var robot = _prefix + '.robot' + _postfix;
+ var normal = (tv2_testsite ? _prefix + '.' + tv2_testsite + '.tv2.dk'
+ : live_sitename + 'tv2.dk');
+ links.push(new this.TV2Link('opdatering_swap', (tv2_sitetype!='opdatering')?opdatering:normal, '/'));
+ //links.push(new this.TV2Link('template_swap', (tv2_sitetype!='template')?template:normal, '/'));
+ links.push(new this.TV2Link('robot_swap', (tv2_sitetype!='robot')?robot:normal, '/'));
+
+ links.push(new this.TV2LinkSplit());
+
+ // Add the ViewCVS link, inspired by Adrian Bak (ADBA)
+ var viewcvs = 'http://viewcvs.tv2.dk:7467/cgi-bin/viewvc.cgi/'
+ + cvs_sitename + '.tv2.dk/' + (tv2_sitetype?tv2_sitetype:'webroot');
+ var php = this._reg_php.exec(tv2_uriOnly);
+ if (php) {
+ viewcvs += php[1] + '?view=log';
+ } else if(tv2_uriOnly.substr(-1) == '/') {
+ // we add 'index.php' and say that's it! :-)
+ viewcvs += tv2_uriOnly + 'index.php' + '?view=log';
+ } else {
+ viewcvs += tv2_uriOnly;
+ }
+ // we set a nice type so we can swap between using the button (test<>viewcvs, live<>viewcvs,...)
+ var viewcvs_type;
+ if (!tv2_testsite) {
+ viewcvs_type = 'viewcvs_live';
+ } else if (tv2_testtype == 'snapshot') {
+ viewcvs_type = 'viewcvs_snapshot';
+ } else {
+ viewcvs_type = 'viewcvs_test';
+ }
+ links.push(new this.TV2LinkWithLabel(viewcvs_type, this.strs.getString('lookupInViewCVS'), viewcvs));
+
+ // Update and I2 base URL
+ var updatepostfix = (tv2_user ? '.' + tv2_user : '')
+ + '.opdatering'
+ + (tv2_testsite ? '.' + tv2_testsite : '2') + '.tv2.dk'
+ var i2link = 'http://i2' + updatepostfix;
+
+ // Check if we are on an Tango or I2 interface, then we can extract the URL again
+ var onI2interface;
+ var onI2interface_url;
+ if (tv2_sitetype == 'opdatering' && tv2_sitename == 'i2') {
+ var tangoif = this._reg_fromi2if.exec(tv2_uri);
+ if (tangoif) {
+ onI2interface = tangoif[1];
+ onI2interface_url = decodeURIComponent(tangoif[3]);
+ }
+ }
+
+ // Add Tango lookup link
+ var i2tango = i2link + '/tango/entry.php?url=';
+ var tango = encodedURL;
+ var docWin = getBrowser().contentWindow;
+ if (docWin.wrappedJSObject && docWin.wrappedJSObject.Tango_URL) {
+ tango = encodeURIComponent(docWin.wrappedJSObject.Tango_URL);
+ links.push(new this.TV2LinkWithLabel('tango/dynamic', this.strs.getString('viewDynamicVersion'),
+ i2link + '/tango/dynamic.php?url='+tango+'&referer='+encodedURL, 'flushCache'));
+ }
+ if (onI2interface != 'tango/entry' && onI2interface != 'tango/requeue') {
+ links.push(new this.TV2LinkWithLabel('tango/entry', this.strs.getString('lookupInTango'),
+ i2link + '/tango/entry.php?url=' + tango));
+ links.push(new this.TV2LinkWithLabel('tango/requeue', this.strs.getString('requeueInTango'),
+ i2link + '/tango/requeue.php?url=' + tango));
+ }
+
+ // Add run pdo_log link
+ if (onI2interface != 'tool/pdo_log/frameset') {
+ links.push(new this.TV2LinkWithLabel('tool/pdo_log/frameset', this.strs.getString('performDbPdoLog'),
+ i2link + '/tool/pdo_log/frameset.php?url='+tango+'&autostop=1&prefix='+initials, 'flushCache'));
+ }
+
+ if (onI2interface_url) {
+ /* link entered in I2 interface */
+ links.push(new this.TV2LinkSplit());
+ /* tango/entry, tango/requeue, tango/dynamic, tool/pdo_log */
+ links.push(new this.TV2LinkWithLabel(onI2interface, onI2interface_url, onI2interface_url));
+ }
+
+ // Some special cases for some sites
+ if (tv2_sitename == 'ttv') {
+ links.push(new this.TV2LinkSplit());
+ var ttv_page = /side=([0-9]+)/.exec(tv2_uri);
+ if (ttv_page) {
+ skip_treenodes = true;
+ links.push(new this.TV2LinkWithLabel('ttvpage', this.strs.getString('lookupTTVPage') + ' ' + ttv_page[1],
+ i2link + '/tool/ttv/?1=' + ttv_page[1] + '&2=0')); /* hardcoded to TV 2 TTV */
+ }
+ /*links.push(new this.TV2Link('ttvpumpe', 'ttvpumpe'+updatepostfix, '/'));*/
+ links.push(new this.TV2Link('ttvpumpe', 'ttvpumpe.opdatering.tv2.dk', '/'));
+ }
+
+ // I2-Files and I2-Images
+ if (tv2_sitename == 'i2-files' || tv2_sitename == 'i2-images') {
+ var contentId = this._reg_i2files.exec(tv2_uriOnly);
+ if (contentId) {
+ var typeId;
+ var typeName;
+ skip_treenodes = true;
+ if (tv2_sitename == 'i2-files') {
+ typeId = 74; // I2_File (and subtypes)
+ typeName = 'I2_File';
+ } else {
+ if (tv2_uriOnly.substr(0,2)=='/s') {
+ typeId = 8; typeName = 'I2_Image_Selection';
+ } else {
+ typeId = 7; typeName = ' I2_Image';
+ }
+ }
+ links.push(new this.TV2LinkSplit());
+ links.push(new this.TV2LinkWithLabel('i2files', this.strs.getString('lookupContent')+ ' ' + contentId[1] + ', ' +
+ this.strs.getString('lookupContent.type') + ' ' + typeName,
+ i2link +
+ '/tool/query/?id=&_checkbox=1&2=&3='+typeId+'&content_id='+contentId[1]+'&4=&5=0&6=&action=Query&timeout=1'));
+ }
+ }
+
+ // Try to extract node id and add links to the node tool
+ if (!skip_treenodes) {
+ var i2node = i2link + '/tool/node/?id=';
+ var nodeids;
+ var first = true;
+ while ((nodeids = this._reg_nodeid.exec(tv2_uri)) != null) {
+ if (first) {
+ links.push(new this.TV2LinkSplit());
+ first = false;
+ }
+ links.push(new this.TV2LinkWithLabel('node', this.strs.getString('lookupTreeNode') + ' ' + nodeids[1],
+ i2node + nodeids[1]));
+ }
+ }
+
+ } else {
+
+ /* Add links to the site if viewing info at ViewCVS site */
+ var fromviewcvs = this._reg_fromviewcvs.exec(currentURL);
+ if (fromviewcvs) {
+ var tv2_sitename = fromviewcvs[1];
+ var tv2_sitetype = fromviewcvs[2];
+ var tv2_uri = fromviewcvs[3];
+
+ // cut off 'index.php'
+ if (tv2_uri.substr(-9) == 'index.php') {
+ tv2_uri = tv2_uri.substr(0, tv2_uri.length-9);
+ }
+
+ // php5 sites
+ //var php5 = ((!tv2_sitename) || (this._reg_php5.test(tv2_sitename)));
+ var php5 = ((tv2_sitename) && (this._reg_php5.test(tv2_sitename)));
+ var testType = php5 ? '' : '3';
+
+ var live_sitename = (tv2_sitetype=='webroot' && tv2_sitename=='www') ? '': (tv2_sitename+'.');
+ var type = (tv2_sitetype=='webroot')? '' : tv2_sitetype+'.';
+
+ links.push(new this.TV2Link('viewcvs_live', live_sitename + type + 'tv2.dk', tv2_uri));
+ links.push(new this.TV2Link('viewcvs_test', tv2_sitename + '.' + initials + '.' + type + 'test' + testType + '.tv2.dk', tv2_uri));
+ links.push(new this.TV2Link('viewcvs_snapshot', tv2_sitename + '.' + type + 'snapshot' + testType + '.tv2.dk', tv2_uri));
+ links.push(new this.TV2Link('viewcvs_testprompt', tv2_sitename + '.?.' + type + 'test' + testType + '.tv2.dk', tv2_uri, 'promptInitials'));
+
+ } else {
+ links.push(new this.TV2LinkWithLabel('disabled', this.strs.getString('notTV2Site'), null, '', true));
+ links.push(new this.TV2LinkSplit());
+ links.push(new this.TV2Link('tv2dk', 'tv2.dk', '/'));
+ }
+ }
+
+ // Utility links
+ links.push(new this.TV2LinkSplit());
+ links.push(new this.TV2LinkWithLabel('link_tree', this.strs.getString('i2Tree'), 'http://i2.opdatering2.tv2.dk/tree/'));
+ links.push(new this.TV2LinkWithLabel('node', this.strs.getString('nodeInformationTool'), 'http://i2.opdatering2.tv2.dk/tool/node/'));
+ links.push(new this.TV2LinkWithLabel('link_query', this.strs.getString('nodeQueryTool'), 'http://i2.opdatering2.tv2.dk/tool/query/'));
+ links.push(new this.TV2LinkWithLabel('tango/entry', this.strs.getString('tangoInterface'), 'http://i2.opdatering2.tv2.dk/tango/'));
+ links.push(new this.TV2LinkWithLabel('w3c', this.strs.getString('w3c'),
+ 'http://validator.w3.org/check?uri=' + encodedURL));
+ return links;
+
+ },
+
+ fillMenu: function(event) {
+ var menu = event.target;
+ var links = this.getLinks();
+ this.emptyMenu(menu);
+
+ var defSet = false;
+ for (var i=0; i<links.length; i++) {
+ defSet |= this.addMenuLink(menu, links[i], defSet);
+ }
+ },
+
+ handleMenu: function(event) {
+ var t = event.target;
+ this.gotoLink(event, t.getAttribute('url'),
+ t.getAttribute('tv2link'),
+ t.getAttribute('tv2flags'));
+ },
+
+ getButtonAction: function() {
+ /* repeat last action or may hit a fall back link of same type */
+ var children = this.getLinks();
+ for (var i = 0; i < children.length; i++) {
+ if (children[i].type == this._lastAction) {
+ return children[i];
+ }
+ }
+ return null;
+ },
+
+ handleButton: function(event) {
+ var action = this.getButtonAction();
+ if (action) {
+ this.gotoLink(event, action.url, action.type, action.flags);
+ } else {
+ event.target.open = true;
+ }
+ },
+
+ setStatusText: function(text) {
+ var statusTextFld = document.getElementById("statusbar-display");
+ if (statusTextFld && statusTextFld.label != text) {
+ statusTextFld.label = text;
+ }
+ },
+
+ handleStatusText: function(event) {
+ var id = event.target.getAttribute('id');
+ if (id == 'tv2-swap-button') {
+ var action = this.getButtonAction();
+ var button = document.getElementById('tv2-swap-button');
+ if (action) {
+ button.setAttribute('tooltiptext', action.label);
+ this.setStatusText(action.url);
+ } else {
+ button.removeAttribute('tooltiptext');
+ }
+ } else {
+ var url = event.target.getAttribute('url');
+ if (url) {
+ this.setStatusText(url);
+ }
+ }
+ },
+
+ handleKeyShortcut: function(event) {
+ document.getElementById('tv2-swap-button').open = true;
+ },
+
+ gotoLink: function(event, url, tv2linktype, tv2flags) {
+ if (url) {
+ /* handle flags */
+ if (tv2flags) {
+ if (tv2flags.indexOf('promptInitials') != -1) {
+ var initials = prompt(this._strs.getString('enterInitials'), this.getPref('alternativ-initials', ''));
+ if (initials == '' || initials == null) {
+ return; // cancelled
+ }
+ this.setPref('alternativ-initials', initials);
+ url = url.replace('?', initials);
+ }
+ if (tv2flags.indexOf('flushCache') != -1) {
+ this.clearCache();
+ }
+ }
+ /* follow link */
+ openUILink(url, event, false, false, false); /* allow ctrl, alt, but don't google */
+ if (tv2linktype) {
+ this._lastAction = tv2linktype;
+ this.setPref('lastaction-linktype', tv2linktype);
+ }
+ }
+ },
+
+ /* function to clear cache */
+ clearCache: function () {
+ var cacheService = Components.classes["@mozilla.org/network/cache-service;1"]
+ .getService(Components.interfaces.nsICacheService);
+ try {
+ cacheService.evictEntries(Components.interfaces.nsICache.STORE_ANYWHERE);
+ } catch(ex) {}
+ },
+
+}
+
+window.addEventListener("load", function(e) { TV2Developer.init(); }, false);