Skrolli 2016.3 - Tuunaa Tulikettua
LISTAUS 1 (index.js):
var pageMod = require("sdk/page-mod");
var ui = require("sdk/ui");
var tabs = require("sdk/tabs");
var panels = require("sdk/panel");
var menuButton = ui.ToggleButton(
{
id: "menutoggle",
label: "Show counting menu",
icon: {
"16":"./s-icon.png",
"32":"./s-icon.png",
"64":"./s-icon.png"
},
onChange: handleChange
});
function handleChange(state) {
if (state.checked) {
countingMenu.show({
position: menuButton
});
}
}
var countingMenu = panels.Panel({
contentURL: "./content_html/countingMenu.html",
contentScriptFile: "./content_scripts/countingMenu.js",
onHide: handleHide
});
function handleHide(){
menuButton.state('window', {checked: false});
}
countingMenu.port.on('count-requested', function(keyword){
var countWorker = tabs.activeTab.attach({
contentScriptFile: "./content_scripts/countall.js"
});
countWorker.port.emit('count-words', keyword);
});
var skrolliPageMod = null;
countingMenu.port.on('pagemod-enabled-changed', function(enabled){
if(enabled)
{
skrolliPageMod = pageMod.PageMod({
include: '*.skrolli.fi',
contentScriptFile: './content_scripts/countall.js',
onAttach: function(worker){
worker.port.emit('count-words', '[Ss]krolli');
}
});
}
else
{
skrolliPageMod.destroy();
}
});
LISTAUS 2 (countingMenu.html):
LISTAUS 3 (countingMenu.js):
var wordToCount = document.getElementById("keyword");
var pageModCheckbox = document.getElementById("enable");
var countButton = document.getElementById("countButton");
countButton.disabled = true;
pageModCheckbox.addEventListener('change', function(){
self.port.emit('pagemod-enabled-changed', pageModCheckbox.checked);
});
wordToCount.addEventListener('input', function(){
countButton.disabled = (wordToCount.value.length <= 0);
});
countButton.addEventListener('click', function(){
self.port.emit('count-requested', wordToCount.value);
});
LISTAUS 4 (countall.js):
var wordToCount = document.getElementById("keyword");
function getTextNodes(parent)
{
var nodes = [];
var walker = document.createTreeWalker(parent,
NodeFilter.SHOW_TEXT,
null,
false);
var node;
while(node=walker.nextNode())
{
nodes.push(node);
}
return nodes;
}
function countWord(word)
{
var elements = getTextNodes(document.body);
var count = 0;
var matcher = new RegExp(word,'g');
[].forEach.call(elements, function(e,i,l){
count += (e.textContent.match(matcher) || []).length;
});
alert('Word "' + word + '" occurred ' + count + ' times.');
}
self.port.on('count-words', countWord);
LINKIT:
https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs
https://developer.mozilla.org/en-US/Add-ons/SDK/Low-Level_APIs
https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials
https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Module_structure_of_the_SDK
https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles#w_starting-the-profile-manager
https://developer.mozilla.org/en-US/Add-ons/SDK/Tools/console#Logging_Levels
http://kb.mozillazine.org/About:config#Adding_modifying_and_resetting_preferences
https://github.com/mozilla/addon-sdk/wiki/Community-developed-modules
http://betterexplained.com/articles/how-to-make-a-bookmarklet-for-your-web-application/
http://www.bookmarklets.com/tools/categor.html
https://www.squarefree.com/bookmarklets/