The code is:

(function(window, undefined ) { var w; if (typeof unsafeWindow != undefined){ w = unsafeWindow } else { w = window; } if (w.self != w.top){ return; } if (/https?:\/\/(www\.)?linux.org.ru/.test(w.location.href)){ //Содержание } })(window); 

It is necessary to install the functions that will be run from html, which will be added by the script, as well as from each other.

If you push these functions into the content or you specify it on the main level in the script, the browser (I check it in the chrome only) doesn’t see them at startup.

Naturally, I usually write:

 function foo (a, b) { //Содержание } 

How to set functions in userscripts?


upd. Decision:

 function contentEval(source) { source = '(' + source + ')();' var script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = source; document.body.appendChild(script); document.body.removeChild(script); } contentEval (function() { window.function1 = function(a,b){ //... }; window.function2 = function(c,d){ //... }; }); 
  • interesting) <br> not tried, but it might work like this: translate = function (obj) {return function () {for (var key in obj) window [key] = obj [key]; }} tr = {abc: function () {}, bcd: function () {}}; setTimeout (translate (tr), 1); - timka_s
  • Brr ... Something did not understand nifga. And where to push the function? - moscwich
  • This is actually UserScript, in the variable real - there will be a real window - timka_s

1 answer 1

Here is an example !!! what you want:

 (function( w ) { if (w.self != w.top){ return; } if (/https?:\//.test(w.location.href)){ w.document.body.innerHTML += '<div '+ 'style = "'+ 'position:absolute;'+ 'top:100px;'+ 'left:500px;'+ 'background-color:red;'+ 'width:200px;'+ 'height:200px;'+ '"'+ 'id = "test"'+ '>'+ '<center>test_of_click</center>'+ '</div>'; w.document.getElementById('test').onclick = function(){ alert('test working'); } } })( window || unsafeWindow ); 

Add something new in the main namespace - I did not succeed ...

  • From here he took: habrahabr.ru/blogs/javascript/129343 How do you suggest - will this solve the subject? - moscwich 2:29
  • Unfortunately, not that. Everything is already written - you just need to fasten. There function from many objects is caused with different arguments. - moscwich