0

My userscript has the following code

var scrpt=document.getElementsByTagName('script');

i included this to know the number of scripts of each page i access.

This works fine with some websites but for some sites i am not getting all the scripts present. I installed the user script in both firefox and chrome the issue is the number of scripts for the same site is different in both browsers.

For example when i access this link Help extracting text from html tag with Java and Regex

i am getting the number of scripts in firefox as:17 and in chrome as:15

but when i view the page source there are 22 script tags

Please help me to slove the problem. I even tried document.scripts but still i get the same result.

Can i know why this happens

1
  • Perhaps these other script tags are created later. Consider that script tag generation can be dynamic and different from browser to browser, from execution to execution. Like.. a blog software can create a new script tag for every "Like in facebook" button in every article listed. Commented Apr 3, 2012 at 11:34

3 Answers 3

0

You don't view the page source, you inspect your DOM, right? The source code includes 18 <script> tags (for me, hand-counted), but there will be others which are dynamically loaded (getting 22 when executing document.getElementsByTagName('script').length in console).

So when is your userscript (Opera or FF's greasemonkey?) executed? onDOMready not all scripts need to be availble. In Opera I would hook a counter on the onBeforeScript event.

That you are getting different counts in ff and chrome may happen because you are not viewing the same page. The html returned to you will be influenced by login status (cookies) and maybe the browser string. Especially dynamically loaded scripts may differ between browser.

Sign up to request clarification or add additional context in comments.

3 Comments

My user script executes when the page is loaded
Yes, and from then it is not triggered when other scripts are inserted. You may hook an a DOMmanipulation event to catch them. What does your script do with the number?
can you give an example of using DOM manipulation event to catch scripts
0

A script is not the content of its tag, a script tag is more like an instruction to the browser to download and evaluate the file at its src.

Script tags can be added, removed, and reused with new src attributes, not to mention scripts that can be loaded and evaluated with ajax.

In a way, a document has only one script, no matter how many files are added from different sources.

Comments

0

You could start by using your existing approach to count the number of scripts available on page load, followed by a hook on DOMNodeInserted or DOMNodeInsertedIntoDocument to count the scripts inserted by other scripts (loaded asynchronously). However, hooking into DOMNodeInserted can make pages very slow, considering your custom handler is fired every. single. time. something is added to the DOM.

What I would recommend would be to set your calculation to start say, half a second later than it does.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.