Mozilla FireFox Code Copy Add On - QB64 Wiki

Mozilla FireFox Code Copy Add On

From QB64 Wiki

Jump to: navigation, search

Some members have complained about not being able to copy the WIKI code examples using the Mozilla Firefox browser. When code is copied from an example and saved in a text editor, the code is pasted as one continuous line of text with no line breaks.

After months of searching we have finally found a solution to the problem. We wish to thank members NG, CodeGuy and others who contributed their efforts in finding the solution to the copy problem.

NOTE: QB64, its owner and members are NOT resposible for any damages resulting from the use or misuse of the process mentioned below!
By downloading the software package the user agrees and assumes all risk!


The Grease Monkey Mozilla Firefox Script Add-on:

  • Step 1) Download the "Grease Monkey" add-on available below. This requires installing the program and adding it to Firefox and is more invasive. However the add-on allows you to change the look and functionality of other web pages also. Read more information at their site: http://www.greasespot.net/
GreaseMonkey download


  • Step 2) After the installation, a monkey face should appear in the upper or lower right corner of the Firefox window. Right-click on it (or left-click the small down arrow that is just to the right of the monkey face) and a menu should appear. Choose New User Script and give the script a name, leave the namespace as it is and add a description if you wish, if the namespace is empty you can type anything you like in that box (it must not be empty).


  • Step 3) In some older Fire Fox versions the Includes section may have two link lines. Enter: http://qb64.net/wiki/* and http://www.qb64.net/wiki/*. This limits the use of the add-on to just the QB64 WIKI! An editor (the same chosen during installation), will appear with some rows already filled in by the Grease Monkey setup wizard. Append the following code AFTER the existing code that Greasemonkey creates:

var allLinks;
allLinks = document.evaluate('//p[substring(@style,1,12)="padding: 1em"]',
//    '//p[@style="padding: 1em; line-height:100%; margin-left:10px; white-space: pre; font-family: Courier; background-color: black; color: lightgrey;"]',
                             document, null,
                             XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
                             null);

for (var i = 0; i < allLinks.snapshotLength; i++) {
    var thisLink = allLinks.snapshotItem(i);
    var createElt;
    if (thisLink.namespaceURI == null)
        createElt = function (eltName) { return document.createElement(eltName); }
    else
        createElt = function (eltName) { return document.createElementNS(thisLink.namespaceURI, eltName); }
    var codeBox = createElt('div');
    codeBox.className = "CodeBox";
    codeBox.style.padding = thisLink.style.padding;
    codeBox.style.margin = thisLink.style.margin;
    codeBox.style.borderBottom = thisLink.style.borderBottom;
    codeBox.style.marginLeft = thisLink.style.marginLeft;
    codeBox.style.fontFamily = thisLink.style.fontFamily;
    codeBox.style.fontSize = thisLink.style.fontSize;
    codeBox.style.backgroundColor = thisLink.style.backgroundColor;
    codeBox.style.lineHeight = thisLink.style.lineHeight;
    codeBox.style.color = thisLink.style.color;
    createCodeBoxContent(thisLink.firstChild, codeBox);
    thisLink.parentNode.insertBefore(codeBox, thisLink.nextSibling);
    thisLink.parentNode.removeChild(thisLink);
}

function createCodeBoxContent(srcElt, destParentElt) {
    while (srcElt != null) {
        if (srcElt.nodeType == document.TEXT_NODE) {
            var txt = srcElt.nodeValue;
            var textArray = new Array();
            txt = txt.replace(/\t/g, '        ');
            txt = txt.replace(/ /g, String.fromCharCode(0xA0));
            textArray = txt.split('\n');
            for (var i = 0; i < textArray.length; i++) {
                destParentElt.appendChild(document.createTextNode(textArray[i]));
                if (i + 1 < textArray.length) {
                    destParentElt.appendChild(createElt('br'));
                }
            }
        } else {
            destParentElt.appendChild(srcElt.cloneNode(false));
            createCodeBoxContent(srcElt.firstChild, destParentElt.lastChild);
        }
        srcElt = srcElt.nextSibling;
    }
}


Script was made by chronokitsune


If the above code does not copy correctly, go to the bottom of page 6 of this topic in the QB64 Forum for the code:
http://www.qb64.net/forum/index.php?topic=1092.75
  • Step 4) Save the code written so far and make sure that the Enabled box is checked. Then try copying the TEST code in grey below.
NOTE: You may have to restart the browser for the script to take effect.
The new page may have an aspect slightly different than what you are used to!
The GreaseMonkey solution allows users to copy code or text directly from any web page.


TEST Code Example (should copy exactly as displayed!)

var allLinks; allLinks = document.evaluate('//p[substring(@style,1,12)="padding: 1em"]', // '//p[@style="padding: 1em; line-height:100%; margin-left:10px; white-space: pre; font-family: Courier; background-color: black; color: lightgrey;"]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); for (var i = 0; i < allLinks.snapshotLength; i++) { var thisLink = allLinks.snapshotItem(i); var createElt; if (thisLink.namespaceURI == null) createElt = function (eltName) { return document.createElement(eltName); } else createElt = function (eltName) { return document.createElementNS(thisLink.namespaceURI, eltName); } var codeBox = createElt('div'); codeBox.className = "CodeBox"; codeBox.style.padding = thisLink.style.padding; codeBox.style.margin = thisLink.style.margin; codeBox.style.borderBottom = thisLink.style.borderBottom; codeBox.style.marginLeft = thisLink.style.marginLeft; codeBox.style.fontFamily = thisLink.style.fontFamily; codeBox.style.fontSize = thisLink.style.fontSize; codeBox.style.backgroundColor = thisLink.style.backgroundColor; codeBox.style.lineHeight = thisLink.style.lineHeight; codeBox.style.color = thisLink.style.color; createCodeBoxContent(thisLink.firstChild, codeBox); thisLink.parentNode.insertBefore(codeBox, thisLink.nextSibling); thisLink.parentNode.removeChild(thisLink); } function createCodeBoxContent(srcElt, destParentElt) { while (srcElt != null) { if (srcElt.nodeType == document.TEXT_NODE) { var txt = srcElt.nodeValue; var textArray = new Array(); txt = txt.replace(/\t/g, ' '); txt = txt.replace(/ /g, String.fromCharCode(0xA0)); textArray = txt.split('\n'); for (var i = 0; i < textArray.length; i++) { destParentElt.appendChild(document.createTextNode(textArray[i])); if (i + 1 < textArray.length) { destParentElt.appendChild(createElt('br')); } } } else { destParentElt.appendChild(srcElt.cloneNode(false)); createCodeBoxContent(srcElt.firstChild, destParentElt.lastChild); } srcElt = srcElt.nextSibling; } }



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page
Personal tools