UserScript • PB-SyntaxHighlighting for codes in the forum

For everything that's not in any way related to PureBasic. General chat etc...
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: UserScript • PB-SyntaxHighlighting for codes in the foru

Post by ts-soft »

skywalk wrote:Hey, I have a dumb question...
Is there a way to select the Code portion of a post without having to manually select everything?
I see buttons for this in other forums, just curious why it is not available on a coder's forum?
Download this: http://www.realsource.de/index.php/down ... codefolder
Install only the "CodeFold for PureBasic Forum".

Or use this:

Code: Select all

// ==UserScript==
// @name              	CodeFold for PureBasic Forums
// @description      	CodeFold
// @include      	http://purebasic.fr/*
// @include      	http://www.purebasic.fr/*
// @include      	http://purebasic.com/*
// @include      	http://www.purebasic.com/*
// @include      	http://forums.purebasic.fr/*

// ==/UserScript== 


var hoch    	= "95px";

var select  	= "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAANCAMAAABIK2QJAAAAB3RJTUUH2AYGEg8u3H74gAAAABd0RVh0U29mdHdhcmUAR0xEUE5HIHZlciAzLjRxhaThAAAACHRwTkdHTEQzAAAAAEqAKR8AAAAEZ0FNQQAAsY8L/GEFAAADAFBMVEVFRUWAAAAAgACAgAAAAICAAIAAgIDAwMDA3MCmyvAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/+/CgoKSAgID/AAAA/wD//wAAAP//AP8A//////+NCwMjAAAAAWJLR0T/pQfyxQAAADZJREFUeJyNjEEKADAMwvz/h+fNWdeOHRekSJCCjSTUMUT6MqKQLqdm4zM7PrP2t+fRvx+Ihw1yH39YLwn1QAAAAABJRU5ErkJggg==";
var minus   	= "data:image/gif;base64,R0lGODlhCgAKAKIAADMzM//M/93d3WZmZv///wAAAAAAAAAAACH5BAEHAAEALAAAAAAKAAoAAAMVGLrc/mrISQe5+ErBOx9AKI5QaS4JADs=";
var plus    	= "data:image/gif;base64,R0lGODlhCgAKAKIAADMzM//M/93d3WZmZv///wAAAAAAAAAAACH5BAEHAAEALAAAAAAKAAoAAAMgGDo8+mEQ4mAUosaGG5sEBjJYWQ5ACmAq8AyddWVy+yQAOw==";


var expandfunc =  '\n';
expandfunc +=     'function expand_code(img_id)\n';
expandfunc +=     '{\n';
expandfunc +=     ' var node = document.getElementById(img_id);\n';
expandfunc +=     ' node.src = "'+minus+'" ;\n';
expandfunc +=     ' node.onclick = function() { collapse_code(img_id); };\n';
expandfunc +=     ' var node = document.getElementById("c"+img_id);\n';
expandfunc +=    '  node.style.overflow = "visible";\n';
expandfunc +=    '  node.style.height = "100%";\n';
expandfunc +=     '}\n';

var collapsefunc = '\n';
collapsefunc +=    'function collapse_code(img_id)\n';
collapsefunc +=    '{\n';
collapsefunc +=    ' var node = document.getElementById(img_id);\n';
collapsefunc +=    ' node.src = "'+plus+'" ;\n';
collapsefunc +=    ' node.onclick = function() { expand_code(img_id); };\n';
collapsefunc +=    ' var node = document.getElementById("c"+img_id);\n';
collapsefunc +=    ' node.style.height = "'+hoch+'";\n';
collapsefunc +=    ' node.style.overflow = "scroll";\n';
collapsefunc +=    '}\n';

var selectfunc  = '\n';
selectfunc     += 'function select_code ( img_id )\n';
selectfunc     += '{\n';
selectfunc     += ' if ( window.getSelection() && window.getSelection().removeAllRanges() ) {\n';
selectfunc     += '   window.getSelection().removeAllRanges();\n';
selectfunc     += ' }\n';
selectfunc     += ' div_id = img_id - 1000;\n';
selectfunc     += ' var node = document.getElementById ( "c" + div_id );\n';
selectfunc     += ' if ( document.createRange() ){;\n';
selectfunc     += '   range = document.createRange();\n';
selectfunc     += '   if ( range.selectNode(node) ){;\n';
selectfunc     += '     range.selectNode(node);\n';
selectfunc     += '   };\n';
selectfunc     += ' };\n';
selectfunc     += ' if ( window.getSelection() && window.getSelection().addRange( range ) ){\n';
selectfunc     += '   window.getSelection().addRange( range );\n';
selectfunc     += ' };\n';
selectfunc     += '}\n';


var img_id 		= 0;
var len = document.getElementsByTagName("div").length;
var script;

for (var i = 0;i<len;i++)
{
	if (document.getElementsByTagName("div")[i].className == "codetitle")
	{
		var node = document.getElementsByTagName("div")[i];
		var html = node.innerHTML;       
        var count = 0;				
		var code = 0;
		
		node.innerHTML = '<b>Code :</b>';
		
		img_id += 1; 
		select_id = img_id + 1000;
		
		code = node.nextSibling;
		
		if (code.innerHTML.split("<br>").length - 1 >= 10)
		{
			node.innerHTML += '&nbsp;<img id="'+ img_id    +'" src="'+plus  +'" onclick="expand_code('+img_id   +')" />';
			
			code.style.height = hoch;        
			code.style.overflow = "scroll";        
		}		
			
		code.id = "c" + img_id;  
			
		node.innerHTML += '&nbsp;<img id="'+ select_id +'" src="'+select+'" onclick="select_code('+select_id+')" />';
	}
}

script = document.createElement( 'script' );
script.innerHTML  = expandfunc; 
script.innerHTML += collapsefunc; 
script.innerHTML += selectfunc; 

document.body.appendChild(script);
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
skywalk
Addict
Addict
Posts: 3960
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: UserScript • PB-SyntaxHighlighting for codes in the foru

Post by skywalk »

STARGÅTE wrote:triple click in the Code-Area (for IE)
Ah, I am using Chrome. Always seems last to the party :(

@ts-soft, Thanks, I will give your code a try...
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
skywalk
Addict
Addict
Posts: 3960
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: UserScript • PB-SyntaxHighlighting for codes in the foru

Post by skywalk »

Thanks ts-soft!
Wow, what a timesaver. This should be default on the forum.

Question?
I don't know javascript.
How to edit the "rs.pbf.codefold.user.js" script to copy the selection directly to the ClipBoard?

I did a web search and it seems a bit complicated.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: UserScript • PB-SyntaxHighlighting for codes in the foru

Post by ts-soft »

skywalk wrote:How to edit the "rs.pbf.codefold.user.js" script to copy the selection directly to the ClipBoard?
This is a security risk and not supported :wink:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Nituvious
Addict
Addict
Posts: 998
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: UserScript • PB-SyntaxHighlighting for codes in the foru

Post by Nituvious »

This is very cool! Thank you!

In a later version, could it be possible to change the coloring system so we can just paste our color preferences from the IDE into the script?
▓▓▓▓▓▒▒▒▒▒░░░░░
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: UserScript • PB-SyntaxHighlighting for codes in the foru

Post by c4s »

Nituvious wrote:This is very cool! Thank you!

In a later version, could it be possible to change the coloring system so we can just paste our color preferences from the IDE into the script?
:?:
Run the second code and you'll get your color settings of the IDE. Then just copy the result into the script file (edit it with rightclick in the script manager).
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Re: UserScript • PB-SyntaxHighlighting for codes in the foru

Post by flaith »

Hi guys, just an update because of the new keywords

I modified the file in greasemonkey, here is the part to be changed in the file pure_basic_syntax_highli.user.js :
var PBSH_Syntax = /([^\w";\\][ \t]*(?:Align|And|Array|As|Break|CallDebugger|Case|CompilerCase|CompilerDefault|CompilerElse|CompilerElseIf|CompilerEndIf|CompilerEndSelect|CompilerError|CompilerIf|CompilerEndIf|CompilerSelect|CompilerEndSelect|Continue|Data|DataSection|EndDataSection|Debug|DebugLevel|Declare|DeclareC|DeclareCDLL|DeclareDLL|Default|Define|Dim|DisableASM|DisableDebugger|DisableExplicit|Else|ElseIf|EnableASM|EnableDebugger|EnableExplicit|End|EndDataSection|EndEnumeration|EndIf|EndImport|EndInterface|EndMacro|EndProcedure|EndSelect|EndStructure|EndStructureUnion|EndWith|Enumeration|EndEnumeration|Extends|FakeReturn|For|Next|ForEach|Next|ForEver|Global|Gosub|Goto|If|EndIf|Import|EndImport|ImportC|EndImport|IncludeBinary|IncludeFile|IncludePath|Interface|EndInterface|List|Macro|EndMacro|MacroExpandedCount|Map|NewList|NewMap|Next|Not|Or|Procedure|EndProcedure|ProcedureC|EndProcedure|ProcedureCDLL|EndProcedure|ProcedureDLL|EndProcedure|ProcedureReturn|Protected|Prototype|PrototypeC|Read|ReDim|Repeat|Until|Restore|Return|Select|EndSelect|Shared|Static|Step|Structure|EndStructure|StructureUnion|EndStructureUnion|Swap|Threaded|To|UndefineMacro|Until|Wend|While|Wend|With|EndWith|XIncludeFile|XOr)(?!\w))|(;[^\r\n]*(?=[\r\n]))|(\#\w*\$?|'[^'\r\n]*')|("[^"\r\n]*")|([^\w.][ \t]*\w+\$?(?=(?:[ \t]*\.[ \t]*\w+[ \t]*|[ \t]*)\())|([\n][ \t]*![^\r\n;]*(?=[\n\r;]))|(.\@\*?[\w\$]*|[^\w$)\]][ \t]*\*[\w\$]+|.\?\w*)|(\W(?:\d+\.?[e\d]*|\$[\dabcdef]+|\%[01]+)(?!\w))|(\.[ \t]*(?:[^\Wabcdfilqsuw](?!\w)|\w\w+)|\\[ \t]*\w+\$?|\W\w+(?=[ \t]*\\)|\W\w+(?=[ \t]*\.[ \t]*(?:[^\Wabcdfilqsuw]\W|\w\w+)))|([\n][ \t]*\w+\$?(?=[ \t]*:))|([+*/\-|!%=~]|&|<|>)|([()\[\]\\:,.])/gi ;
“Fear is a reaction. Courage is a decision.” - WC
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: UserScript • PB-SyntaxHighlighting for codes in the foru

Post by c4s »

@flaith
Thank you!

One question though: When I saved the modified source code GreaseMonkey said something about "all scripts should use @grant". Is this important?
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: UserScript • PB-SyntaxHighlighting for codes in the foru

Post by Bisonte »

c4s wrote:One question though: When I saved the modified source code GreaseMonkey said something about "all scripts should use @grant". Is this important?
I add this

Code: Select all

// @grant         none
before

Code: Select all

// ==/UserScript==
to prevent this message.
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: UserScript • PB-SyntaxHighlighting for codes in the foru

Post by helpy »

I use a the prosivler style in the forum.
This means that the script does not work, because the html code is different.

I changed the function PureBasicSyntaxHighlighting of the script:

Code: Select all

 function PureBasicSyntaxHighlighting()
  {
  // Anwenden der Syntaxhervorhebung auf alle Elemente mit der Klasse codecontent
  // apply the syntax highlighting on all the elements with class codecontent
  var allMy_dl = document.getElementsByTagName('dl');
  for(var i = 0; i < allMy_dl.length; i++)
   {
   if (allMy_dl[i].className == 'codebox')
    {
    var aCodeBox = allMy_dl[i].getElementsByTagName('code')[0];
    if (PBSH_Highlight['EditorFontName'])
     aCodeBox.style.font = PBSH_Highlight['EditorFontStyle']+" "+PBSH_Highlight['EditorFontSize']+"pt "+PBSH_Highlight['EditorFontName'];
    if (PBSH_Highlight['BackgroundColor'])
     aCodeBox.style.backgroundColor = PBSH_Highlight['BackgroundColor'];
    if (PBSH_Highlight['NormalTextColor'])
     aCodeBox.style.color           = PBSH_Highlight['NormalTextColor'];
    var code = ' '+aCodeBox.innerHTML+'\r';
    code = code.replace(/<br>/gi, '\r\n');
    code = code.replace(/&nbsp;/gi, '\t');
    code = code.replace(/<\/?[^<>]*>/gi, '');
    code = code.replace(PBSH_Syntax, PBSH_Replace);
    code = code.replace(/\t/gi, '&nbsp;');
    code = code.replace(/\r\n/gi, '<br>');
    aCodeBox.innerHTML = code;
    }
   }
  }
Here a sample of my highlighting:
Image

Und hier mein geändertes Script: PureBasic Syntax Highlighting (Fork of STARGÅTE's script)

cu,
guido
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: UserScript • PB-SyntaxHighlighting for codes in the foru

Post by c4s »

I updated flaith's code for PB5.20 by adding "DeclareModule", "EndDeclareModule", "EndModule", "Module" and "Runtime". Furthermore I properly sorted the keywords and removed about 20 duplicates:
var PBSH_Syntax = /([^\w";\\][ \t]*(?:Align|And|Array|As|Break|CallDebugger|Case|CompilerCase|CompilerDefault|CompilerElse|CompilerElseIf|CompilerEndIf|CompilerEndSelect|CompilerError|CompilerIf|CompilerSelect|Continue|Data|DataSection|Debug|DebugLevel|Declare|DeclareC|DeclareCDLL|DeclareDLL|DeclareModule|Default|Define|Dim|DisableASM|DisableDebugger|DisableExplicit|Else|ElseIf|EnableASM|EnableDebugger|EnableExplicit|End|EndDataSection|EndDeclareModule|EndEnumeration|EndIf|EndImport|EndInterface|EndMacro|EndModule|EndProcedure|EndSelect|EndStructure|EndStructureUnion|EndWith|Enumeration|Extends|FakeReturn|For|ForEach|ForEver|Global|Gosub|Goto|If|Import|ImportC|IncludeBinary|IncludeFile|IncludePath|Interface|List|Macro|MacroExpandedCount|Map|Module|NewList|NewMap|Next|Not|Or|Procedure|ProcedureC|ProcedureCDLL|ProcedureDLL|ProcedureReturn|Protected|Prototype|PrototypeC|ReDim|Read|Repeat|Restore|Return|Runtime|Select|Shared|Static|Step|Structure|StructureUnion|Swap|Threaded|To|UndefineMacro|Until|UnuseModule|UseModule|Wend|While|With|XIncludeFile|XOr)(?!\w))|(;[^\r\n]*(?=[\r\n]))|(\#\w*\$?|'[^'\r\n]*')|("[^"\r\n]*")|([^\w.][ \t]*\w+\$?(?=(?:[ \t]*\.[ \t]*\w+[ \t]*|[ \t]*)\())|([\n][ \t]*![^\r\n;]*(?=[\n\r;]))|(.\@\*?[\w\$]*|[^\w$)\]][ \t]*\*[\w\$]+|.\?\w*)|(\W(?:\d+\.?[e\d]*|\$[\dabcdef]+|\%[01]+)(?!\w))|(\.[ \t]*(?:[^\Wabcdfilqsuw](?!\w)|\w\w+)|\\[ \t]*\w+\$?|\W\w+(?=[ \t]*\\)|\W\w+(?=[ \t]*\.[ \t]*(?:[^\Wabcdfilqsuw]\W|\w\w+)))|([\n][ \t]*\w+\$?(?=[ \t]*:))|([+*/\-|!%=~]|&|<|>)|([()\[\]\\:,.])/gi ;
edit: Added "UseModule" and "HideModule"
edit2: Renamed "HideModule" to "UnuseModule"
Last edited by c4s on Sun Aug 18, 2013 12:00 pm, edited 3 times in total.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: UserScript • PB-SyntaxHighlighting for codes in the foru

Post by Tenaja »

ts-soft wrote:With Firefox, if you have installed Greasemonkey, simple download the file. Firefox will prompt for installation.
Ok, so I installed GreaseMonkey (in Firefox), and installed the script. Nothing. I restarted FF. Still none of the code is highlighted. Am I missing something?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: UserScript • PB-SyntaxHighlighting for codes in the foru

Post by ts-soft »

Here a complete pack of all required script.
Put this files in your gm_scripts dir of the greasemonkey-plugin (in Appdata/Modzilla/...).
It should show you all PB-Sources in blue-style :wink: , PB5.20 is supported.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: UserScript • PB-SyntaxHighlighting for codes in the foru

Post by Kwai chang caine »

There are a long time, i use this super JS on my chome for select all the code in one clic 8)
http://www.purebasic.fr/english/viewtop ... 96#p347396
Now i try to install it another time and chrome disable it, and say to me contact the developper.
He must inscribe his code in Chrome Web Store for be sure it's not a malware :shock:

Decidedly..the more time passes, the more difficult it is to do what you want in my own explorer :?
ImageThe happiness is a road...
Not a destination
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: UserScript • PB-SyntaxHighlighting for codes in the foru

Post by ts-soft »

Kwai chang caine wrote:He must inscribe his code in Chrome Web Store for be sure it's not a malware :shock:
Then do this, or use firefox.

If no one do this, i can't do this. I haven't chrome installed.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Post Reply