« Tutorial Plugin : 4 - Astuces » : différence entre les versions

De Wiki Francophone de Maxthon
Hpsam (discussion | contributions)
Ernest (discussion | contributions)
Aucun résumé des modifications
Ligne 3 : Ligne 3 :
It's recommended to put a toolbar plugin inside an anonymous function.
It's recommended to put a toolbar plugin inside an anonymous function.
This makes sure that you don't get problems with variables and it makes sure that websites can't use your functions and security_id.
This makes sure that you don't get problems with variables and it makes sure that websites can't use your functions and security_id.
<i>
Il est recommandé de mettre un plugin de barre d'outils à l'intérieur d'une fonction anonyme. Vous serez ainsi certain de ne pas avoir de problèmes avec les variables et avec les sites Web qui ne pourront pas utiliser vos fonctions et vos "security_id".
</i>
An anonymous function looks like this:
An anonymous function looks like this:
<pre>
<pre>

Version du 4 octobre 2005 à 07:25

It's recommended to put a toolbar plugin inside an anonymous function. This makes sure that you don't get problems with variables and it makes sure that websites can't use your functions and security_id.


Il est recommandé de mettre un plugin de barre d'outils à l'intérieur d'une fonction anonyme. Vous serez ainsi certain de ne pas avoir de problèmes avec les variables et avec les sites Web qui ne pourront pas utiliser vos fonctions et vos "security_id".

An anonymous function looks like this:

(funtion() {

//your code

}())

The example in part 1 in an anonymous function is:

<script language="JavaScript">

(funtion() {
var pr=confirm('Would you like to go activate tab number 2?')
if(pr==true) external.activate_tab(%max_security_id,2)
}())

</script>

Now websites can't use your variable 'pr' and the websites will still work if they are also using a variable called 'pr'.Note: You have to put 'var' before the variable to make it a local variable!

If you use max_activex or any other new Maxthon command, make sure that the users gets an error message if he doesn't have the correct Maxthon version. For example:

try{
external.m2_callerName(%max_security_id,'PLUGINNAME')
window.open('about:blank','_blank')
var XML=external.get_tab(%max_security_id,
external.cur_sel).external.max_activex( %max_security_id, 
"Microsoft.XMLDOM")
external.get_tab(%max_security_id,external.cur_sel).close()
}
catch(err){ 
alert('Sorry. This plugin only works with Maxthon 1.2.1 and higher!') 
}

Snippets

Here are some Javascript Snippets that you can use in your plugins: Get the selected text, also on frames. Toolbar Version:

var keyword=''
//document.frames.length is the number of frames
for(i=0;i< document.frames.length;i++) {
//the try/catch part is for external frames. 
//They cause an error (acces denied) 
//and this code ignores that error
try {
keyword += document.frames[i].document.selection.createRange().text
}
catch(err){}
}
//if a page doesn't have frames
keyword+=document.selection.createRange().text 

Sidebar Version:

//active tab
var doc = external.get_tab(max_security_id, external.cur_sel).document 
var keyword=''

//doc.frames.length is the number of frames

for(i=0;i< doc.frames.length;i++) {
//the try/catch part is for external frames. 
//They cause an error and this code ignores that error

try {keyword += doc.frames[i].document.selection.createRange().text}
catch(err){}
}
//if a page doesn't have frames
keyword+=doc.selection.createRange().text 

Create an ActiveX object (thanks to That1ComputerGuy)

function createAX(obj) {
var tempWin = window.open('about:blank','') //open a window
//create an activeX object in the new window
var axObj = tempWin.external.max_activex(%max_security_id, obj)
tempWin.close() //close the window
return axObj //return the ActiveX object
}

// The following code is not necessary, it's just for testing purposes

//run the function above
var xmlDoc = createAX('Microsoft.XMLDOM'); 
//wait untill the complete XML document is loaded
xmlDoc.async = false; 
//load the xml document
xmlDoc.load('http://www.w3schools.com/xml/note.xml') 
//alert the document
alert(xmlDoc.xml);

Sidebar plugins don't need this funtion, they can use var xmlDoc = external.max_activex(max_security_id,'Microsoft.XMLDOM') directly Load/save (ini)files easier with these functions:

sid=%max_security_id
pluginname='PLUGINNAME'

function WriteFile(FileName,Data) {
external.writeFile(sid,pluginname,FileName,Data)
}
function ReadFile(FileName) {
return external.readFile(sid,pluginname,FileName)
}
function WriteIni(Key, Data) {
external.m2_writeIni(sid, pluginname,"plugin.ini","Settings", Key, Data)
}
function ReadIni(Key, Default) {
return external.m2_readIni(sid, 
pluginname,"plugin.ini","Settings", Key, Default)
}

Now you can read/write files like this:

//write 'thisIsATest' to text.txt, located in the plugin folder
WriteFile('text.txt','thisIsATest') 

//alert the content of 'text.txt'
alert(ReadFile('text.txt')) 

//alert the 'test' key in plugin.ini, 
//return 'not yet defined!' if the key is not available
alert(ReadIni('test','not yet defined!')) 

//write key 'test'
WriteIni('test','defined') 

//alert the content of key 'test', return '' if not available 
//(shouldn't happen now, 
//since you have just written 'defined' to the key)
alert(ReadIni('test','')) 

For sidebar plugins you should replace %max_security_id with max_security_id

Insert an external javascript into a webpage This can be useful if you insert a button into a webpage and want a function to be executed if the user clicks the button.

//create an element

elmnt=document.createElement('<script src="'+
external.m2_plugin_folder(%max_security_id, 'PLUGINNAME')+ 
'test.js"><\/script>');


//Add this element into the 'head' of a webpage
document.documentElement.appendChild(elmnt);

This will insert 'test.js' located in your plugins folder into a webpage. NOTE: you get a security alert if you are on a HTTPS website, like gmail.com. For sidebar plugins you should replace %max_security_id with max_security_id

Upload your plugin

You should compress your plugin into a .zip archive. All files need to be inside a folder. If you don't put your files inside a folder, users have to do that themself and that is not very efficient/friendly. It's not recommended to put the files in a .rar archive, since not everybody can decompress those files. You can safely remove the 'max.src' and 'thumbs.db' files from the archive if they exist.

If you update your plugin don't forget to update the version number too, to avoid confusion.

Other Plugins

These plugins can be useful for web developers:

  • ViewPage By Ptma (view the source of a webpage, extra all images, view meta data and more)
  • DevArt By Arthur R (view the source, view all tags, validate webpages and more)

Problems?

If you have a problem with your plugin, or you don't know how to do something you can always view the source of other plugins.This is an excellent way to start programming, just modify another plugin.You really learn a lot by just looking and understanding somebody's code!But if you use a lot of code from another plugin don't forget to ask permission to the author of that plugin!

Questions?

You can ask all plugin questions on the Maxthon Forum.You can post your comments about this tutorial in this topic.


© Copyright 2005. Tutorial made by Neo101. Version 1.12 01-09-2005


< Partie 3: Commandes de Maxthon | Sommaire Plugin | Partie 5 : Exemples >