We add two events into AutoPager from version 0.6.0.8. There are AutoPagerBeforeInsert and AutoPagerAfterInsert.
AutoPagerBeforeInsert is triggered on the hidden document loaded by AutoPager before import the content to the visible page. You can get the the element from event.target.
AutoPagerAfterInsert is triggered on the target page users are viewing. You can get the the element from event.target.
We will suggest you use AutoPagerAfterInsert to process the added content in your own script. For example, you can add the necessary event handlers missed while import from one document to another caused by this bug.
For example, this is an example greakmonkey script to log the autopager activities:
// ==UserScript==
// @name apTest
// @namespace apTest
// @include *
// ==/UserScript==
document.addEventListener("AutoPagerBeforeInsert", function (e){GM_log("AutoPagerBeforeInsert:" + e.target)}, true)
document.addEventListener("AutoPagerAfterInsert", function (e){GM_log("AutoPagerAfterInsert:" + e.target)}, true)
One important tips is that document.getElementById may not work well since there will have elements with same ids after AutoPager merge content from following pages.
You may need find a new way to generate ids to make them unique cross pages. Otherwise you need to use getElementsByTagName or xpath to get the elements you need.
Here is an xpath example:
var nodes = document.evaluate("id('yourid')", document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null );
var node = nodes.iterateNext();
while (node) {
//work on the node
node = nodes.iterateNext();
}
It's easy to modify your scripts to make it works with AutoPager if you already you 'load' event like this:
window.addEventListener('load', function (e) {onxxload();}, false);
You need just copy the line and change the event name to AutoPagerAfterInsert:
window.addEventListener('load', function (e) {onxxload();}, false);
window.addEventListener('AutoPagerAfterInsert', function (e) {onxxload();}, false);
For example, you can make two changes to make Flickr Functional Suite works well with AutoPager.
1. Append this line to the end of the script :
window.addEventListener('AutoPagerAfterInsert', function (e) {CatFlickrSuite.init();}, false);
2. Modify the id generating code to make it generating unique id for different page.
Here is the diff:
--- 5016.user.js 2010-03-02 19:14:54.456030689 +0800
+++ flickr_functional_suite.user.js 2010-03-02 19:13:49.828030742 +0800
@@ -166,6 +166,7 @@
colBuf: 64,
counter: 0,
ticket: 0,
+ randomPrefix : Math.random() + "-",
ticketStatus: new Object(),
// Special links that we want to ignore:
specialLinks: ['1', '< Prev', 'You', 'Organize', 'Upload', 'Popular Tags',
@@ -928,7 +929,7 @@
// The function is both a setter and a getter
if (!el || !tag) return null;
// If the element does not have an id assigned, do so now:
- if (!el.id) el.id = "CatFlickrSuite" + ++this.counter;
+ if (!el.id) el.id = "CatFlickrSuite" + this.randomPrefix + ++this.counter;
var key = el.id;
// Initialize the hash if this is the first access on the element:
if (!this.privateData[key]) this.privateData[key] = new Object();
@@ -1015,7 +1016,7 @@
// Clear out private variables associated with an element
if (!el) return;
// If the element does not have an id assigned, do so now:
- if (!el.id) el.id = "CatFlickrSuite" + ++this.counter;
+ if (!el.id) el.id = "CatFlickrSuite" + this.randomPrefix + ++this.counter;
var key = el.id;
var rv = this.privateData[key]
// Totally wipe the settings:
@@ -1093,3 +1094,5 @@
// This is the call that starts the ball rolling - it launches the
// init() method when the page finishes loading:
window.addEventListener('load', function (e) {CatFlickrSuite.init();}, false);
+window.addEventListener('AutoPagerAfterInsert', function (e) {CatFlickrSuite.init();}, false);
+
The original version
The modified version
One more tuning can be made to make it process only the new inserted nodes in AutoPagerAfterInsert handler:
The tuned version
--- 5016.user.js 2010-03-02 19:14:54.456030689 +0800
+++ flickr_functional_suite_tunned.user.js 2010-03-02 19:25:56.272031668 +0800
@@ -166,6 +166,7 @@
colBuf: 64,
counter: 0,
ticket: 0,
+ randomPrefix : Math.random() + "-",
ticketStatus: new Object(),
// Special links that we want to ignore:
specialLinks: ['1', '< Prev', 'You', 'Organize', 'Upload', 'Popular Tags',
@@ -179,10 +180,10 @@
elipsed: /\.\.\.$/, // Trailing 3 periods (elipses)
nsid: /^\d+\@.{3}$/, // Not sure what the real format is?
},
- init: function() {
+ init: function(node) {
// The primary initialization function
//GM_log("Starting: " + new Date());
- this.setEnvironment();
+ this.setEnvironment(node);
this.initTransmute();
this.setSettings();
this.insertSettings();
@@ -193,14 +194,14 @@
this.finalize();
//GM_log("finished");
},
- setEnvironment: function() {
+ setEnvironment: function(node) {
// Gather some global information
var href = this.env.href = document.location.href;
var mat = href.match(this.re.urlPool1);
// See if we are in a group pool-related page:
if (!mat) mat = href.match(this.re.urlPool2);
if (mat) this.env.pool = mat[1];
- this.links = document.getElementsByTagName("a");
+ this.links = node.getElementsByTagName("a");
},
initTransmute: function() {
// Turn one object type into another This just allows
@@ -928,7 +929,7 @@
// The function is both a setter and a getter
if (!el || !tag) return null;
// If the element does not have an id assigned, do so now:
- if (!el.id) el.id = "CatFlickrSuite" + ++this.counter;
+ if (!el.id) el.id = "CatFlickrSuite" + this.randomPrefix + ++this.counter;
var key = el.id;
// Initialize the hash if this is the first access on the element:
if (!this.privateData[key]) this.privateData[key] = new Object();
@@ -1015,7 +1016,7 @@
// Clear out private variables associated with an element
if (!el) return;
// If the element does not have an id assigned, do so now:
- if (!el.id) el.id = "CatFlickrSuite" + ++this.counter;
+ if (!el.id) el.id = "CatFlickrSuite" + this.randomPrefix + ++this.counter;
var key = el.id;
var rv = this.privateData[key]
// Totally wipe the settings:
@@ -1092,4 +1093,6 @@
};
// This is the call that starts the ball rolling - it launches the
// init() method when the page finishes loading:
-window.addEventListener('load', function (e) {CatFlickrSuite.init();}, false);
+window.addEventListener('load', function (e) {CatFlickrSuite.init(document);}, false);
+window.addEventListener('AutoPagerAfterInsert', function (e) {CatFlickrSuite.init(e.target);}, false);
+
Example for addons:
function consoleLog (message) {
var consoleService = Components.classes['@mozilla.org/consoleservice;1']
.getService(Components.interfaces.nsIConsoleService);
consoleService.logStringMessage(message)
}
function examplePageLoad(source,node) {
consoleLog(source + ":" + node.tagName)
}
window.addEventListener("load", function() {
gBrowser.addEventListener("DOMContentLoaded",function(){
examplePageLoad("DOMContentLoaded",content.document.documentElement);
}, false);
}, false);
window.addEventListener("AutoPagerAfterInsert",function(e){
examplePageLoad("AutoPagerAfterInsert",e.target);
},false);
Another example for GM script:
Splash album viewer Fixed
window.addEventListener('AutoPagerBeforeInsert', function(e){
//remove "is" attr which is attached by the same script run in the hidden browser
var nodes = e.target.getElementsByTagName("img");
for(var i=0;i<nodes.length;i++)
{
nodes[i].removeAttribute("is")
}
},false);
window.addEventListener('AutoPagerAfterInsert', function(){
album_finder_img();
},false);
|