diff --git a/dreditor.user.js b/dreditor.user.js index 5955810..47f3042 100644 --- a/dreditor.user.js +++ b/dreditor.user.js @@ -1368,8 +1368,13 @@ Drupal.behaviors.dreditorIssueAddEdit = function(context) { * Fetches the node to retrieve the original poster's name and profile link. */ var getPoster = function() { - // Load the issue's view page. - $.get($('#tabs a:contains("View")').attr('href'), function(data) { + // Try to find a path to the node from the URL. + var node_path = location.href.match(/^.*node\/[0-9]*/); + + // Only proceed if a valid node path was found. + if (node_path) { + // Load the issue's view page. + $.get(node_path[0], function(data) { // Find the link to the original poster's profile. var link = $(data).find('div.node > div.submitted a'); @@ -1377,15 +1382,13 @@ Drupal.behaviors.dreditorIssueAddEdit = function(context) { var new_link = '' + link.text() + ''; $('#edit-body').val($('#edit-body').val().replace(/\[username\]/, new_link)); }); + } }; /** * Fetches and inserts the issue summary template from the handbook. - * - * @param edit_form - * TRUE if we are on a node edit form; FALSE if it is a node add form. */ - var injectTemplate = function(edit_form) { + var injectTemplate = function() { // Load the issue summary instructions. $.get('http://drupal.org/node/1155816', function(data) { // We need the first codeblock to extract the template. @@ -1398,25 +1401,21 @@ Drupal.behaviors.dreditorIssueAddEdit = function(context) { // Prepend text to current body. $('#edit-body').val(text + $('#edit-body').val()); - // If we are on the node edit form, append the poster link. - if (edit_form) { - getPoster(); + // If we are editing a new issue, remove the "original report" header. + if (location.pathname.search('node/add') >= 0) { + $('#edit-body').val($('#edit-body').val().replace(/

.*<\/h3>/, '')); } - // Otherwise, we are editing a new issue, so remove the "original report" - // header. This is the original report. + // Otherwise, try to add a link to the original poster's profile. else { - $('#edit-body').val($('#edit-body').val().replace(/

.*<\/h3>/, '')); + getPoster(); } }); }; /** * Adds the 'insert template' button above the issue summary field. - * - * @param edit_form - * TRUE if we are on a node edit form; FALSE if it is a node add form. */ - var addTemplateButton = function(edit_form) { + var addTemplateButton = function() { // We want to append the button in the label area. var $label = $('label[for="edit-body"]'); @@ -1427,7 +1426,7 @@ Drupal.behaviors.dreditorIssueAddEdit = function(context) { .text("Insert template") .attr('class', 'dreditor-button') .click(function() { - injectTemplate(edit_form); + injectTemplate(); return false; } ); @@ -1442,22 +1441,10 @@ Drupal.behaviors.dreditorIssueAddEdit = function(context) { $instructions.after(')'); }; - // This markup appears only on node edit forms. - $('body.logged-in.page-node.node-type-project-issue #edit-body').once('dreditorIssueTemplate', function() { - // Add the template button and pass a true value (1) for edit_form. - // This will ensure the pasted template is appropriate for an existing - // issue. - addTemplateButton(1); - }); - - // This markup appears on both node add and edit forms. - // However, on node edit forms, the once() for dreditorIssueTemplate has - // already run, so it will not run again. + // Add the template button. $('body.logged-in.page-node div.project-issue #edit-body').once('dreditorIssueTemplate', function() { - // Add the template button and pass a false value (0) for edit_form. - // This will ensure the pasted template is appropriate for a new issue. - addTemplateButton(0); - }); + addTemplateButton(); + }); }; /**