Greasy Fork

Gazelle Quick Quote

Only selected text is quoted as long as it's quoted in the proper quote box

目前为 2017-02-21 提交的版本。查看 最新版本

// ==UserScript==
// @name Gazelle Quick Quote
// @namespace itismadness
// @description Only selected text is quoted as long as it's quoted in the proper quote box
// @author Chrome version by Mordred (inspired by Etheryte and Amareus)

// @include https://*apollo.rip/inbox.php?action=viewconv&id=*
// @include https://*apollo.rip/forums.php?*action=viewthread&threadid=*
// @include https://*apollo.rip/torrents.php?id=*
// @include https://*apollo.rip/colleges.php?id=*
// @include https://*apollo.rip/artist.php*
// @include https://*apollo.rip/requests.php?action*

// @include https://*passtheheadphones.me/inbox.php?action=viewconv&id=*
// @include https://*passtheheadphones.me/forums.php?*action=viewthread&threadid=*
// @include https://*passtheheadphones.me/torrents.php?id=*
// @include https://*passtheheadphones.me/colleges.php?id=*
// @include https://*passtheheadphones.me/artist.php*
// @include https://*passtheheadphones.me/requests.php?action*

// @include https://*redacted.ch/inbox.php?action=viewconv&id=*
// @include https://*redacted.ch/forums.php?*action=viewthread&threadid=*
// @include https://*redacted.ch/torrents.php?id=*
// @include https://*redacted.ch/colleges.php?id=*
// @include https://*redacted.ch/artist.php*
// @include https://*redacted.ch/requests.php?action*

// include https://*passthepopcorn.me/inbox.php?action=viewconv&id=*
// include https://*passthepopcorn.me/forums.php?*action=viewthread&threadid=*
// include https://*passthepopcorn.me/torrents.php?id=*
// include https://*passthepopcorn.me/colleges.php?id=*
// include https://*passthepopcorn.me/artist.php*
// include https://*passthepopcorn.me/requests.php?action*

// @require https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
// @version 0.9.9
// @date 2016-02-21
// ==/UserScript==
{
    function addJQuery(callback) {
        var script = document.createElement("script");
        script.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js");
        script.addEventListener('load', function() {
            var script = document.createElement("script");
            script.textContent = "(" + callback.toString() + ")();";
            document.body.appendChild(script);
        }, false);
        document.body.appendChild(script);
    }

    function main() {
        var orig_quote = window.Quote;
        window.Quote = function(args) {
            return false;
        };

        $('a[onclick^="Quote("]').click(function(event) {
            //Get required arguments
            var arguments = jQuery(this).attr("onclick");
            var username = arguments.replace(/Quote\(\'[0-9]*\', \'/, "").replace(/Quote\(\'[0-9]*\',\'/, "").replace(/\'.*/, "");
            var postID = arguments.replace(/Quote\(\'/, "").replace(/\'.*/, "");
            var textArea = jQuery('#quickpost');
            var addNewLines = false;
            var link = true;
            var target = '';
            var requrl = '';

            var elem = getSelectionContainerElement(); // find what element contains the selection and only quote if it's the container for the Quote button you clicked on
            var selText = window.getSelection().toString();

            if (url.path == "inbox") {
                requrl = 'inbox.php?action=get_post&post=' + postID;
            } else {
                requrl = 'comments.php?action=get&postid=' + postID;
            }
            if (url.path == "artist") {
                // artist comment
                target = 'a';
            } else if (url.path == "torrents") {
                // torrent comment
                target = 't';
            } else if (url.path == "collages") {
                // collage comment
                target = 'c';
            } else if (url.path == "requests") {
                // request comment
                target = 'r';
            } else {
                // forum post
                requrl = 'forums.php?action=get_post&post=' + postID;
            }
            target += postID;
            /*
            if(selText.length != 0 && (elem.id == "content"+postID || (elem && elem.id == "" && elem.nodeName != "BLOCKQUOTE" && elem.parentNode.id == "content"+postID))){
            // the target and requrl code got moved above so that the links work in more than just the forums
            var target = '';
            var requrl = '';

            ajax.get(requrl, function(response) {
            debugger;
            var selText = window.getSelection().toString();
            var selection = window.getSelection();

            if ($('#quickpost').raw().value !== '') {
            $('#quickpost').raw().value = $('#quickpost').raw().value + "\n\n";
            }
            str = html_entity_decode(response);
            $('#quickpost').raw().value = $('#quickpost').raw().value + "[quote=" + username + "|" + target + "]" + str + "[/quote]";
            resize('quickpost');
            });
            */

            if (selText.length !== 0 && (elem.id === "content" + postID || elem.id === "message" + postID || (elem && elem.id === "" && elem.nodeName !== "BLOCKQUOTE" && elem.parentNode.id === "content" + postID))) {
                if (textArea.val() !== '') addNewLines = true;
                var val = textArea.val() + (addNewLines ? "\n\n" : "") + "[quote=" + username;
                if (url.path != "inbox") {
                    val += "|" + target;
                }
                val += "]" + selText.trim() + "[/quote]";
                textArea.val(val);

                resize('quickpost');
                //...Otherwise proceed with a regular full-post quote
            } else {
                if (url.path != "inbox") {
                    orig_quote(postID, username, true);
                }
                else {
                    orig_quote(postID, username);
                }
            }
            textArea[0].scrollIntoView();

            return false;
        });

        function getSelectionContainerElement() {
            var range, sel, container;
            if (document.selection && document.selection.createRange) {
                // IE case
                range = document.selection.createRange();
                return range.parentElement();
            } else if (window.getSelection) {
                sel = window.getSelection();
                if (sel.getRangeAt) {
                    if (sel.rangeCount > 0) {
                        range = sel.getRangeAt(0);
                    }
                } else {
                    // Old WebKit selection object has no getRangeAt, so
                    // create a range from other selection properties
                    range = document.createRange();
                    range.setStart(sel.anchorNode, sel.anchorOffset);
                    range.setEnd(sel.focusNode, sel.focusOffset);

                    // Handle the case when the selection was selected backwards (from the end to the start in the document)
                    if (range.collapsed !== sel.isCollapsed) {
                        range.setStart(sel.focusNode, sel.focusOffset);
                        range.setEnd(sel.anchorNode, sel.anchorOffset);
                    }
                }

                if (range) {
                    /*** This function is generic and unedited except for the part between here and the next comment. Remove this section to always return the actual getSelectionContainerElement. ***/
                    if (range.startContainer.parentNode.nodeName == "BLOCKQUOTE" || range.endContainer.parentNode.nodeName == "BLOCKQUOTE") {
                        container = range.commonAncestorContainer.parentNode; // we want to ensure we don't return the true parent div so that later checks will fail
                    } else
                    /*** edited section ends here ***/
                        container = range.commonAncestorContainer;

                    // Check if the container is a text node and return its parent if so
                    return container.nodeType === 3 ? container.parentNode : container;
                }
            }
        }

        /*function doQuickQuote(obj) {
        // can't use $ in here because it's called after we've called jQuery.noConflict
        var addNewLines = false;
        var selText = document.getSelection().toString();
        var commentDiv = jQuery(obj).parent().parent().parent().parent().find('td.body > div').eq(0);
        var ajaxResponse;
        postID = commentDiv.attr("id").match(/content(\d*)/)[1];
        poster = jQuery(obj).parent().find('strong > a').eq(0).text();
        //console.log(poster);
        jQuery.ajax({
        url: "?action=get_post&post=" + postID,
        type: 'GET',
        async: false,
        success: function(response) {
        ajaxResponse = html_entity_decode(response);	//not sure if this needs to happen but Quote() in comments.js does it, so why not be safe?
        }
        });

        elem = getSelectionContainerElement();
        if (selText == "" || selText == null || elem != commentDiv[0]) {
        selText = ajaxResponse;
        }
        textArea = jQuery('#quickpost');
        if (textArea.val() !== '')
        addNewLines = true;
        textArea.val(textArea.val() + (addNewLines ? "\n\n" : "") + "[quote="+poster+"|"+postID+"]" + selText.trim() + "[/quote]");
        }*/

        jQuery.noConflict();
    }
    // load jQuery and execute the main function
    if (/opera/i.test(navigator.userAgent)) {
        console.log("What.CD Quick Quote: If this script is not working in Opera, make sure the filename ends in user.js");
        addJQuery(main);
    } else if (!/firefox/i.test(navigator.userAgent)) { // chrome and safari
        addJQuery(main);
    } else {
        this.$ = this.jQuery = jQuery.noConflict(true);
        main();
    }
}