Can I filter JavaScript by select it from page source ?

Ask for help about NoScript, no registration needed to post
cobal
Posts: 1
Joined: Mon Sep 26, 2011 12:31 pm

Can I filter JavaScript by select it from page source ?

Post by cobal »

Hello,

I am using NS in very basic form, but now I would like to use it to filtering at page to disable selected by myself a part of Java script code which is visible in the page source. I don't know how to do it?

For example, I know I can filter/block scripts which are located in one page, when that scripts were recognized as 'external' scripts (just like "google analitics/urchin"). But how to filter/block a part of script, or all lines of script which are stand alone in page source? I know a pages which had a different scripts in oryginal code, now I want to disallow some part of them to running. I think it is possible by greasmonkey to put additional java in to page and change it on the fly.

Is it possible to do it by NS?


Look here, it is a part of source code of this forum. I would like to change/disallow/filter only some part of that scripts...
1. Can i block a single line of code: " 35 var per_page = '25'; ". This should disallow of the FF to remember that the initial value is 25 for variable 'per_page'.
2. Can i block a few lines of code?
3. Can I block one full selected script (lines 31-73)?
4. Can I block few selected scripts (lines 31-73, and 75)?

Keep in mind that the script which I like to block is located inside this same page and recognized as 'internal'. I know I can block all 'internal' scripts from one page but i want to select only part of them to disallow.


-------------page source-----------
31<script type="text/javascript">
32// <![CDATA[
33 var jump_page = 'Enter the page number you wish to go to:';
34 var on_page = '1';
35 var per_page = '25';
36 var base_url = './viewforum.php?f=23';
37 var style_cookie = 'phpBBstyle';
38 var style_cookie_settings = '; path=/; domain=forums.informaction.com';
39 var onload_functions = new Array();
40 var onunload_functions = new Array();
41
42
43
44 /**
45 * Find a member
46 */
47 function find_username(url)
48 {
49 popup(url, 760, 570, '_usersearch');
50 return false;
51 }
52
53 /**
54 * New function for handling multiple calls to window.onload and window.unload by pentapenguin
55 */
56 window.onload = function()
57 {
58 for (var i = 0; i < onload_functions.length; i++)
59 {
60 eval(onload_functions);
61 }
62 }
63
64 window.onunload = function()
65 {
66 for (var i = 0; i < onunload_functions.length; i++)
67 {
68 eval(onunload_functions);
69 }
70 }
71
72// ]]>
73</script>
74<script type="text/javascript" src="./styles/prosilver/template/styleswitcher.js"></script>
75<script type="text/javascript" src="./styles/prosilver/template/forum_fn.js"></script>

-----------------------------------------
Mozilla/5.0 (Windows; U; Windows NT 6.0; pl; rv:1.9.2.22) Gecko/20110902 Firefox/3.6.22 (.NET CLR 3.5.30729)
Tom T.
Field Marshal
Posts: 3620
Joined: Fri Mar 20, 2009 6:58 am

Re: Can I filter JavaScript by select it from page source ?

Post by Tom T. »

I don't know of any way to do this through NoScript. It would be very complicated to have NoScript changing scripts on the fly.

If you can write a Greasemonkey script to do what you want, that is probably the best way to go.
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.20) Gecko/20081217 Firefox/3.6.23
User avatar
GµårÐïåñ
Lieutenant Colonel
Posts: 3369
Joined: Fri Mar 20, 2009 5:19 am
Location: PST - USA
Contact:

Re: Can I filter JavaScript by select it from page source ?

Post by GµårÐïåñ »

As @Tom T. said, you can use GM (GreaseMonkey and not Giorgio Maone) to write a simple script that would take the document's content, search for the pattern you want gone, remove it, rewrite it in a sense, and load it, resulting in that being gone but keep in mind this would slow down to some extent your loading of those pages, because of the post processing that goes into it, so make sure its worth it. I have tons of GM scripts written for all kinds of things and it automates the hell out of most of the daily things I don't want to spend tediously working on. It does have certain limitations but for the most part you can do just about anything with GM.
~.:[ Lï£ê ï§ å Lêmðñ åñÐ Ì Wåñ† M¥ Mðñê¥ ßå¢k ]:.~
________________ .: [ Major Mike's ] :. ________________
Mozilla/6.9 (Windows NT 6.9; rv:6.9) Gecko/69696969 Firefox/6.9
al_9x
Master Bug Buster
Posts: 931
Joined: Thu Mar 19, 2009 4:52 pm

Re: Can I filter JavaScript by select it from page source ?

Post by al_9x »

cobal wrote:Is it possible to do it by NS?
In many cases you can modify the behavior of scripts (including inline) by altering their code and/or data accessible from global scope via surrogates.

Your per_page example is doable but not very useful. It only affects the go to page # link, the rest of pagination ui (including the most important thing the actual number of posts per page) will remain unaffected and out of sync with go to page link.

Code: Select all

sources = "!@forums.informaction.com/viewtopic.php"
replacement = "per_page=25"
Last edited by al_9x on Sat Oct 15, 2011 3:52 am, edited 1 time in total.
Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20100101 Firefox/8.0
Tom T.
Field Marshal
Posts: 3620
Joined: Fri Mar 20, 2009 6:58 am

Re: Can I filter JavaScript by select it from page source ?

Post by Tom T. »

al_9x wrote:
cobal wrote:Is it possible to do it by NS?
In many cases you can modify the behavior of scripts (including inline) by altering their global variables and replacing global functions via surrogates.]
Clever idea, to do it by creating one's own surrogates. But Yahoo Mail typically runs as many as 120 scripts, and that's just from mail alone. That number includes zero scripts from yahoo.com, and no third-party. So it's going to be an awful lot of work for the OP to do this with each desired script at each site. Wouldn't a pattern-matching GreaseMonkey script be less trouble?
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.23) Gecko/20110920 Firefox/3.6.23
al_9x
Master Bug Buster
Posts: 931
Joined: Thu Mar 19, 2009 4:52 pm

Re: Can I filter JavaScript by select it from page source ?

Post by al_9x »

Tom T. wrote:Wouldn't a pattern-matching GreaseMonkey script be less trouble?
Surrogate sources are patterns. GreaseMonkey has some advantages, scripts are stored as files, unminified, hence easier to maintain, there are various helper functions. But noscript provides greater control over conditions and timing of execution via different surrogate types.
Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20100101 Firefox/8.0
Tom T.
Field Marshal
Posts: 3620
Joined: Fri Mar 20, 2009 6:58 am

Re: Can I filter JavaScript by select it from page source ?

Post by Tom T. »

al_9x wrote:
Tom T. wrote:Wouldn't a pattern-matching GreaseMonkey script be less trouble?
Surrogate sources are patterns. GreaseMonkey has some advantages, scripts are stored as files, unminified, hence easier to maintain, there are various helper functions. But noscript provides greater control over conditions and timing of execution via different surrogate types.
Understood. But each NS surrogate requires two about:config entries, "source" and "replacement", and the replacements are not one-size-fits-all.
So if OP has 20 or 50 sites that he wants to do this to, he has to write than many individual surrogates, make the about:config entries, and do the same every time he encounters a new such site. As GµårÐïåñ suggested, one GM script might suffice to detect the undesired pattern in every script, present or future.

Bottom line is probably whatever OP finds works best for him, which may be dependent on how many sites are affected. The surrogates were still a cool idea, though - thanks for suggesting.
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.23) Gecko/20110920 Firefox/3.6.23
al_9x
Master Bug Buster
Posts: 931
Joined: Thu Mar 19, 2009 4:52 pm

Re: Can I filter JavaScript by select it from page source ?

Post by al_9x »

Tom T. wrote:
al_9x wrote:
Tom T. wrote:Wouldn't a pattern-matching GreaseMonkey script be less trouble?
Surrogate sources are patterns. GreaseMonkey has some advantages, scripts are stored as files, unminified, hence easier to maintain, there are various helper functions. But noscript provides greater control over conditions and timing of execution via different surrogate types.
Understood. But each NS surrogate requires two about:config entries, "source" and "replacement", and the replacements are not one-size-fits-all.
A script will not magically become one-size-fits-all just because you apply it with GM. The matching of scripts to pages is more or less equivalent between NS and GM, both use include/exclude pattern lists. If you write a script that works for example with any stock phpBB forum and want to apply it to multiple sites, NS, AFAICT, is no less capable of doing that. GM has advantages, some of which I mentioned, but this is not one of them.
Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20100101 Firefox/8.0
Tom T.
Field Marshal
Posts: 3620
Joined: Fri Mar 20, 2009 6:58 am

Re: Can I filter JavaScript by select it from page source ?

Post by Tom T. »

al_9x wrote:
Tom T. wrote:Understood. But each NS surrogate requires two about:config entries, "source" and "replacement", and the replacements are not one-size-fits-all.
A script will not magically become one-size-fits-all just because you apply it with GM. The matching of scripts to pages is more or less equivalent between NS and GM, both use include/exclude pattern lists. If you write a script that works for example with any stock phpBB forum and want to apply it to multiple sites, NS, AFAICT, is no less capable of doing that. GM has advantages, some of which I mentioned, but this is not one of them.
I don't use GreaseMonkey myself. Perhaps I misinterpreted GµårÐïåñ's post to imply that GM would search all documents from all sources, and replace as needed on-the-fly. Thanks for elaborating.
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.23) Gecko/20110920 Firefox/3.6.23
User avatar
GµårÐïåñ
Lieutenant Colonel
Posts: 3369
Joined: Fri Mar 20, 2009 5:19 am
Location: PST - USA
Contact:

Re: Can I filter JavaScript by select it from page source ?

Post by GµårÐïåñ »

al_9x wrote:A script will not magically become one-size-fits-all just because you apply it with GM. The matching of scripts to pages is more or less equivalent between NS and GM, both use include/exclude pattern lists. If you write a script that works for example with any stock phpBB forum and want to apply it to multiple sites, NS, AFAICT, is no less capable of doing that. GM has advantages, some of which I mentioned, but this is not one of them.
No, one script never fits all but what you are misunderstanding is Tom's point and mine to some point. Just because there is a cool tool, doesn't mean it should be used as a cureall for everything. Surrogates have their limits, as do GM scripts. Its a balancing act depending on what you are doing, how often, how much of it you are doing and how familiar you are with things. To do surrogates, yes, they are awesome and have NS incharge would be great, but for each instance of the issue you have to create a new surrogate or find a very fine line between a regex that would accomplish a wide range which in itself can lead to many missed opportunities and hits. However, since GM has both before load, DOM load, variable storage and post loading functionality to actually "EDIT" the page content (which is why I said it could be slow depending on how much work is done each time) would allow for a nice regex to be used each time, or in a worse case scenario, having a list of patterns created in an array and adding it in plain single text to be applied using GM is far more efficient in the long term for cosmetic, or small script injections and would run more efficiently from a centralized point and NS would still be able to manage, validate and control its actual script functionality against any exploits. So as you yourself suggested, one solution doesn't fit all and that includes your suggestion of surrogates and its ultimate application here.

Case Study: The OP wants to prevent pagination limits. Surrogate or even GM simple removal of that code, won't affect the actual function of the site (as you also pointed out) and that means there has to be some persistence involved. This can't be accomplished with the surrogates, while a GM ability to hold variable value can be used to enforce the chosen or desired value on each pagination and page invocation whiled removing that limitation via its code replacement. From a pure programming perspective and functionality, which would you say would accomplish that more efficiently? That's the ultimate question here. Not to mention we have no idea the level of experience and ability of the OP to accomplish either solution, we have all pretty much assumed they can. In the case they can't, all this is moot and our clever ideas are just that.

Tangent: You wouldn't always use a .50 cal to shoot everything would you? Say a squirrel? You would use a .22 for that at most. And you wouldn't take a rhino down with buck shots would you? A duck yes, a rhino you need something bigger. The point, in life one size NEVER fits all, it would be naive to think otherwise. Each problem has a "best" solution while many might work, it comes down to analyzing your needs, skill set, ability to implement and efficiency and pick the one that does all that with the least amount of loss of functionality. Sometimes you feel like a surrogate, sometimes just a script will do and vice versa. The least that comes out of this discussion is the availability of experience and perspective given to the OP to decide for themselves.
~.:[ Lï£ê ï§ å Lêmðñ åñÐ Ì Wåñ† M¥ Mðñê¥ ßå¢k ]:.~
________________ .: [ Major Mike's ] :. ________________
Mozilla/6.9 (Windows NT 6.9; rv:6.9) Gecko/69696969 Firefox/6.9
al_9x
Master Bug Buster
Posts: 931
Joined: Thu Mar 19, 2009 4:52 pm

Re: Can I filter JavaScript by select it from page source ?

Post by al_9x »

GµårÐïåñ wrote:but for each instance of the issue you have to create a new surrogate
GM has some legitimate advantages, there is no reason invent fictional ones. As I already explained to Tom, both GM and NS match scripts to pages using url pattern lists, so if you write a script that can be applied to multiple sites, GM offers no advantage in matching.
GµårÐïåñ wrote:However, since GM has ... post loading functionality to actually "EDIT" the page content
What is this functionality?
Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20100101 Firefox/8.0
User avatar
GµårÐïåñ
Lieutenant Colonel
Posts: 3369
Joined: Fri Mar 20, 2009 5:19 am
Location: PST - USA
Contact:

Re: Can I filter JavaScript by select it from page source ?

Post by GµårÐïåñ »

You are obviously not listening and I am not wasting more time explaining it to you, if you read what I wrote, it was detailed enough for ANYONE to get it, but you seem to have blinders on, so I will leave you to it.

http://www.greasespot.net/
http://en.wikipedia.org/wiki/Greasemonkey

might be a good place to start learning.
~.:[ Lï£ê ï§ å Lêmðñ åñÐ Ì Wåñ† M¥ Mðñê¥ ßå¢k ]:.~
________________ .: [ Major Mike's ] :. ________________
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1
Post Reply