将数字值与网页上的单词相关联,然后将其乘以该单词后面的网页上的数字
Associating a number value to a word on a webpage, and then multiplying this by a number on the webpage that follows the word
<img src="http://lawyer.gif" />
<a href="http://lawyer.com">lawyer</a> (111)
或
<img src="http://doctor.gif" />
<a href="http://doctor.com">doctor</a> (222)
因此,有多个页面包含与上述类似的html。我想为"律师"或"律师.gif"或其他任何东西的出现分配一个特定的值,并将其乘以 111 如果律师在页面上,然后将其添加到"医生"乘以 222 IF 医生在页面上。有很多不同的可能的"职业"(~50),所以这里的代码非常简化。一页可以有医生、律师和消防员,而另一页可以只有牙医。然后,此总和应显示在页面上或弹出窗口或其他任何内容中。这里的 111 和 222 来自代码中括号中的数字,这些数字在每一页上都会发生变化。
我计划用 greasemonkey/javascript 来做这件事,但只是因为我在这方面的经验有限。所以我想我的问题是首先,这是否至少可以简单地做,如果是这样,有人至少可以给我一些提示让我开始吗?谢谢。
一般来说:
- 要处理很多事情,请使用数组:
[blah1, blah2, etc.]
要将即席 - 值与即席标签相关联,请使用对象:
[ {blah1: 3.1}, {blah2: 415}, etc.]
- 要检测和操作页面上的内容,请使用jQuery。
- 要向页面添加显示或其他元素,还要使用 jQuery。
综上所述,这是一个完整的脚本,可以完成您的问题似乎要问的问题。 您还可以在jsFiddle上看到代码的运行情况:
// ==UserScript==
// @name Arbitrary math on arbitrary page values
// @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.
*/
var textToValuesArray = [
{"doctor": 3}
, {"lawyer": 5}
, {"rodeo clown": 7}
// etc.
];
var targetNodes = $("td.left:has(a)");
var targetText = targetNodes.text ();
var reportValue = 0;
//-- Loop through the textToValuesArray.
$.each (textToValuesArray, function (index, textToValueObject) {
var textTerm = Object.keys (textToValueObject)[0];
//-- Is the text term in the targeted part of the page?
var termRegex = new RegExp(textTerm, 'i'); // Case insensitive
if (termRegex.test (targetText) ) {
console.log ("Text '" + textTerm + "' was found!");
/*-- Given the indicated page structure, targetText will look like:
"doctor (2)lawyer (4)" etc.
With the value we want coming one space after the target term
and in parentheses.
So, if we split the string on the search term, we get the next
(number), if any.
*/
var splitStr = targetText.split (termRegex);
if (splitStr && splitStr.length > 1) {
var multiplierString = splitStr[1].replace (/^'s*'(('d+)').*$/, "$1");
if (multiplierString) {
var multiplierInteger = parseInt (multiplierString, 10);
if (isNaN (multiplierInteger) ) {
console.log ("Multiplier value not found! (2)");
}
else {
/*-- We found a term and we found its multiplier.
Add to the report value.
*/
var termValue = textToValueObject[textTerm];
console.log (
"termValue: ", termValue,
" ",
"multiplierInteger: ", multiplierInteger
);
reportValue += termValue * multiplierInteger
}
}
else {
console.log ("Multiplier value not found! (1)");
}
}
else {
console.log ("Split string error!");
}
}
} );
console.log ("reportValue: ", reportValue);
$("body").prepend ('<div id="gmReport">The final value was: ' + reportValue + '</div>');
$("#gmReport").css ("background", "orange");
相关文章:
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 测试索引值是否等于某个数字的倍数
- 将数字转换为一定数量的硬币
- 如何防止网页加载后自动启动功能
- 键入最后一位数字后自动提交
- 如何在javascript中迭代数字列表
- Javascript逻辑运算符和字符串/数字
- 如何在javascript中获得与特定数字相等的随机数
- 如果元素's的ID以数字开头
- 如何使用Node.js最有效地解析网页
- 刷新后保留对网页的更改
- 用户在文本框中输入一个数字,并根据使用 PHP 输入的数字重定向到另一个网页
- 使用 javascript 在网页上的同一位置更新数字
- 检查变量是网页上的所有数字javascript
- 使用jQuery从网页中获取包含数字数据的所有元素
- setInterval函数的问题-尝试制作一个数字演示网页
- 是否可以在iOS safari中对网页上的数据进行数字签名
- 将数字值与网页上的单词相关联,然后将其乘以该单词后面的网页上的数字
- 我创建了一个网页,可以将数字分解.我已经创建了该函数.现在我需要创建一个函数来显示我的数学
- 我的网页应该打印出一串数字,但isn'