我是 javascript 的初学者,不知道如何计算一个单词在网页中出现的次数。
我在不同的论坛上进行了研究,但没有得到任何帮助。我非常感谢任何实现此功能的建议或提示。
我是 javascript 的初学者,不知道如何计算一个单词在网页中出现的次数。
我在不同的论坛上进行了研究,但没有得到任何帮助。我非常感谢任何实现此功能的建议或提示。
Four things to be aware of if you want to use a Greasemonkey script to count instances of a word:
Use the special \b
character, in your regex to ensure that you actually get words.
For example, /\bof\b/
matches "of" but not "offer".
Always check that match()
results are not null before trying to access their properties! match(regex).length
will throw an exception much of the time.
Beware that careless scripts can mutually interfere with the web page. This is part of why one of the other answers did not work.
To avoid this, turn Greasemonkey's sandbox back on by specifying a @grant
directive. GM scripts now default to grant none
in many situations!
Beware that many sites, like Google, load content via AJAX, long after the Greasemonkey script fires. There are many strategies to compensate for that. Perhaps the most straightforward is to use a timer.
Putting it all together, here is a complete script that compensates for all of these issues. You can also see the code in action at jsFiddle:
// ==UserScript==
// @name _Show word counts
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
$("body").append ('<div id="gmWordCount"></div>');
checkWordCount (); //-- Initial run, works for static HTML only.
//--- Check for AJAX loaded words... Over twice a sec is plenty fast.
var wordChkTimer = setInterval (checkWordCount, 444);
function checkWordCount () {
//--- Search for "of" as a whole word.
var wordStr = "of";
var wordRegex = new RegExp ("\\b" + wordStr + "\\b", "gi");
var matchRez = $(document.body).text ().match (wordRegex);
var wordCount = matchRez ? matchRez.length : 0;
//--- Display the results.
var countReport = '';
switch (wordCount) {
case 0:
countReport = '"of" was not found!'
break;
case 1:
countReport = '"of" was found one time.'
break;
default:
countReport = '"of" was found ' + wordCount + ' times.'
break;
}
//--- Display results to the user.
$("#gmWordCount").text (countReport);
}
//--- Position and style the display output,
GM_addStyle ( " \
#gmWordCount { \
background: orange; \
position: fixed; \
top: 0; \
left: 0; \
width: 100%; \
z-index: 6666; \
} \
" );
这对你来说是一个开始。就目前而言,它将匹配存在于其他单词中的实例,例如“coffee”将计入“of”,它会干扰其他页面,我没有检查 jQuery 是否已经存在。所以你需要自己做一些工作。
// ==UserScript==
// @name Count words
// @namespace count
// @version 1
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js
// ==/UserScript==
$(function(){
var word='of'; // put your word here
var regex = new RegExp(word, "gi")
alert ($('body').text().match(regex).length);
});
var text = document.body.textContent
.replace(/\r?\n?/g, "") // removes lines
.replace(/\s{2,}/g, " "), // removes duplicate spaces
word = new RegExp("of", "gi");
alert(text.match(word).length);