views:

150

answers:

1

I'm trying to create a generic function that will execute piece of code on the NEXT reload of a page.

This is called by onPageLoad({ onLoad: function() { code to call on next page load goes here } });

The only way I can see of doing this in greasemonkey is to set the whole onLoad: function as a string and set it via GM_setValue then retrieve it on the next page load and eval. Then reset it back to nothing so it doesn't get recalled again on next page load.

Trouble is I get

Error: Greasemonkey access violation: unsafeWindow cannot call GM_setValue.

And for the life of me I can't figure out why!!

Cheers for your help

// ==UserScript==
// @name           test
// @namespace      test
// @include        *
// ==/UserScript==

function start(prop)
{
 var reloadPage = function() { window.location.reload(); };

 //when the page reloads, set another onPageLoad function which alerts 'whooho!!' when it loads again
 var onLoad = 'function() { onPageLoad({ onLoad: function() { alert(\'whoohoo!!\'); }}); window.location.reload(); }';

 onPageLoad({ onLoad: onLoad });

 reloadPage();  //start the chain reaction!
}


function init()
{
 var a = document.createElement('a');
 a.innerHTML = 'start';
 a.setAttribute('href', 'javascript:');
 a.addEventListener('click', start, false); //when clicked start setting the reload functions

 document.body.appendChild(a);

 onPageLoadEvent();
}



function onPageLoad(prop)
{
 GM_setValue('control.onLoad', prop.onLoad); 
}

function onPageLoadEvent()
{
 var onLoad = GM_getValue('control.onLoad'); //get the function to execute

 if (onLoad === undefined || onLoad == '')
 {
  return;
 }

 onPageLoad({ onLoad: '' }); //reset load function so it doesn't get called on next page load

 eval('{('+onLoad+')()}'); //execute the on load function
}

init();
+1  A: 

Does this help?

http://wiki.greasespot.net/Greasemonkey_access_violation

- Jack

Jack