// ==UserScript== // @name Insert APT Link // @author Nathan Osman // @version 1.0.0.5 // @namespace http://quickmediasolutions.com // @description Adds a button to the editing toolbar to make it easy to insert links to software packages. // @include *://askubuntu.com/* // @include *://meta.askubuntu.com/* // ==/UserScript== /* jshint browser:true */ // This function allows us to embed a function on the // page that will immediately get executed. function EmbedFunctionOnPageAndExecute(function_contents) { 'use strict'; var exec_script = document.createElement('script'); exec_script.type = 'text/javascript'; exec_script.textContent = "(" + function_contents.toString() + ")()"; document.getElementsByTagName('head')[0].appendChild(exec_script); } // The first thing we do is inject the toolbar button onto the page EmbedFunctionOnPageAndExecute(function() { 'use strict'; /* globals $, StackExchange */ // Adds the apt button to the toolbar var init_apt_button = function (input, button_row) { // there is no point in attempting to add a button if there is no place // to add it, or if there is no input field if (!input.length || !button_row.length) { return; } // Create a new 'li' with the class 'wmd-button' and append // it to the editing toolbar var img_url = 'http://hostmar.co/software-small'; var button = $('
'); button.click(function() { var old_val = input.val(); var s_start = input[0].selectionStart; var s_end = input[0].selectionEnd; var selection = old_val.substring(s_start, s_end); // This function is used for inserting a APT link in the editor var insert_link = function (data) { // Do nothing if the prompt was cancelled or empty if (!data) { return; } // assume that the last part after a slash contains the package name // example: http://apt.ubuntu.com/p/data http://packages.ubuntu.com/data var package_name = data.split('/').pop(); // The template for the text to be inserted, % will be replaced with the package name var insert_text = "[%](https://apps.ubuntu.com/cat/applications/%)" + "[![Install %](" + img_url + ")](https://apps.ubuntu.com/cat/applications/%)"; insert_text = insert_text.replace(/%/g, package_name); // Compose the new value for the textarea var new_val = old_val.substr(0, s_start) + insert_text + old_val.substr(s_end); // Set the new value input.val(new_val); // Set the focus back and move the cursor position input.focus(); input.selectionStart = s_start + insert_text.length; // Refresh the preview StackExchange.MarkdownEditor.refreshAllPreviews(); }; // Retrieve the selected text var selected_text = old_val.substring(s_start, s_end); if (selected_text) { // insert the link immediately if text is selected insert_link(selected_text); } else { // otherwise display a prompt and ask for the package name. The // insert_link function will be called with the text entered in // the prompt dialog selected_text = window.prompt("Insert APT Link: " + "Please enter the name of the package that you want to embed.", ""); if (selected_text) { insert_link(selected_text); } } }); // Now insert it button_row.append(button); }; // find all input fields and add buttons var init_apt_buttons = function () { // add buttons which are not bound to a post (these do not have an // numeric ID part) init_apt_button($("#wmd-input"), $('#wmd-button-row')); // add buttons to post editors (which have a numeric ID part) $(".wmd-input").each(function(index, element){ // take the numeric part after "wmd-input-" var id = element.id.substr("wmd-input-".length); init_apt_button($(element), $("#wmd-button-row-" + id)); }); }; // This needs to be done after the window has loaded var old_handler = window.onload; window.onload = function() { // Invoke the old handler if(old_handler !== null) old_handler(); // Copied from the StackExchange.editor.initIfShown method: checks if // the editor is visible var editor_is_visible = $("#show-editor-button").length === 0 && $("#wmd-preview").length !== 0; // Add button if the editor is visible if (editor_is_visible) init_apt_buttons(); // Only modify the function if it is available if (StackExchange.editor) { // In case the editor is not visible modify the editor initializer // to add the button var old_init = StackExchange.editor.init; StackExchange.editor.init = function () { // should SE add any extra parameters, the code below can handle it old_init.apply(null, arguments); init_apt_buttons(); }; // Chromium loads the editor after the page has loaded, the above // init_apt_button will therefore not work so modify the function // which is responsible for the editor when loading var old_initIfShown = StackExchange.editor.initIfShown; StackExchange.editor.initIfShown = function () { // should SE add any extra parameters, the code below can handle it old_initIfShown.apply(null, arguments); init_apt_buttons(); }; } }; });