替换变量[Javascript]时出现问题
Having trouble replacing variables [Javascript]
我正在尝试修改谷歌代码Javascript文件,以便获得访问链接的经验。我是javascript的新手,我真的不知道如何表达我想要做的事情。我认为它应该看起来像这样:
var exp = 0 //Current exp
var level = 1; // User Level
var tolvl = level * 100 //Exp to 'level up'
function expchange (exp) {
if (urlToCount <= 1) { //if they have visited the link once today
exp+=10; //Change variable 'exp' to "variable"+10
}
else {
exp+=5; // If more, change to "variable"+5
}
if (exp == tolvl ) { //If Variable 'exp' is equal to Variable 'tolvl'
level+=1; //Change variable 'level' to 'variable'+1
}
//I need some kind of function to write Variables to HTML
我可能做错了什么,很抱歉脚本写得不好。感谢您的帮助
完整的参考代码:
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Event listner for clicks on links in a browser action popup.
// Open the link in a new tab of the current window.
function onAnchorClick(event) {
chrome.tabs.create({
selected: true,
url: event.srcElement.href
});
return false;
}
// Given an array of URLs, build a DOM list of those URLs in the
// browser action popup.
function buildPopupDom(divName, data) {
var popupDiv = document.getElementById(divName);
var ul = document.createElement('ul');
popupDiv.appendChild(ul);
for (var i = 0, ie = data.length; i < ie; ++i) {
var a = document.createElement('a');
a.href = data[i];
a.appendChild(document.createTextNode(data[i]));
a.addEventListener('click', onAnchorClick);
var li = document.createElement('li');
li.appendChild(a);
ul.appendChild(li);
}
}
// Search history to find up to ten links that a user has typed in,
// and show those links in a popup.
function buildTypedUrlList(divName) {
// To look for history items visited in the last week,
// subtract a week of microseconds from the current time.
var microsecondsPerDay = 1000 * 60 * 60 * 24 ;
var onedayAgo = (new Date).getTime() - microsecondsPerDay;
// Track the number of callbacks from chrome.history.getVisits()
// that we expect to get. When it reaches zero, we have all results.
var numRequestsOutstanding = 0;
chrome.history.search({
'text': '', // Return every history item....
'startTime': oneDayAgo // that was accessed less than one week ago.
},
function(historyItems) {
// For each history item, get details on all visits.
for (var i = 0; i < historyItems.length; ++i) {
var url = historyItems[i].url;
var processVisitsWithUrl = function(url) {
// We need the url of the visited item to process the visit.
// Use a closure to bind the url into the callback's args.
return function(visitItems) {
processVisits(url, visitItems);
};
};
chrome.history.getVisits({url: url}, processVisitsWithUrl(url));
numRequestsOutstanding++;
}
if (!numRequestsOutstanding) {
onAllVisitsProcessed();
}
});
// Maps URLs to a count of the number of times the user typed that URL into
// the omnibox.
var urlToCount = {};
// Callback for chrome.history.getVisits(). Counts the number of
// times a user visited a URL by typing the address.
var processVisits = function(url, visitItems) {
for (var i = 0, ie = visitItems.length; i < ie; ++i) {
// Ignore items unless the user typed the URL.
if (visitItems[i].transition != 'link') {
continue;
}
if (!urlToCount[url]) {
urlToCount[url] = 0;
}
urlToCount[url]++;
}
// If this is the final outstanding call to processVisits(),
// then we have the final results. Use them to build the list
// of URLs to show in the popup.
if (!--numRequestsOutstanding) {
onAllVisitsProcessed();
}
};
// This function is called when we have the final list of URls to display.
var onAllVisitsProcessed = function() {
// Get the top scorring urls.
urlArray = [];
for (var url in urlToCount) {
urlArray.push(url);
}
// Sort the URLs by the number of times the user typed them.
urlArray.sort(function(a, b) {
return urlToCount[b] - urlToCount[a];
});
buildPopupDom(divName, urlArray.slice(0, 10));
};
}
document.addEventListener('DOMContentLoaded', function () {
buildTypedUrlList("typedUrl_div");
});
var exp = 0 //Current exp
var level = 1; // User Level
var tolvl = level * 100 //Exp to 'level up'
function expchange (exp) {
if (urlToCount <= 1) { //if they have visited the link once today
exp+=10; //Change variable 'exp' to "variable"+10
}
else {
exp+=5; // If more, change to "variable"+5
}
if (exp == tolvl ) { //If Variable 'exp' is equal to Variable 'tolvl'
level+=1; //Change variable 'level' to 'variable'+1
}
//I need some kind of function to write Variables to HTML
在反复阅读您的代码后,我认为您想要这样的东西。不确定tolvl的计算,但剩下的应该告诉你在哪里以及如何更改
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Event listner for clicks on links in a browser action popup.
// Open the link in a new tab of the current window.
var urlToCount = {};
function onAnchorClick(event) {
chrome.tabs.create({
selected: true,
url: event.srcElement.href
});
return false;
}
// Given an array of URLs, build a DOM list of those URLs in the
// browser action popup.
function buildPopupDom(divName, data) {
var popupDiv = document.getElementById(divName);
var ul = document.createElement('ul');
popupDiv.appendChild(ul);
for (var i = 0, ie = data.length; i < ie; ++i) {
var a = document.createElement('a');
a.href = data[i];
a.appendChild(document.createTextNode(data[i]));
var info = urllToCount[data[i]]
a.addEventListener('click', onAnchorClick);
var li = document.createElement('li');
li.appendChild(a);
li.appendChild(document.createTextNode("- level:"+info.level));
li.appendChild(document.createTextNode("- exp:"+info.ext));
li.appendChild(document.createTextNode("- visits:"+info.visits));
ul.appendChild(li);
}
}
// Search history to find up to ten links that a user has typed in,
// and show those links in a popup.
function buildTypedUrlList(divName) {
// To look for history items visited in the last week,
// subtract a week of microseconds from the current time.
var microsecondsPerDay = 1000 * 60 * 60 * 24 ;
var onedayAgo = (new Date).getTime() - microsecondsPerDay;
// Track the number of callbacks from chrome.history.getVisits()
// that we expect to get. When it reaches zero, we have all results.
var numRequestsOutstanding = 0;
chrome.history.search({
'text': '', // Return every history item....
'startTime': oneDayAgo // that was accessed less than one week ago.
},
function(historyItems) {
// For each history item, get details on all visits.
for (var i = 0; i < historyItems.length; ++i) {
var url = historyItems[i].url;
var processVisitsWithUrl = function(url) {
// We need the url of the visited item to process the visit.
// Use a closure to bind the url into the callback's args.
return function(visitItems) {
processVisits(url, visitItems);
};
};
chrome.history.getVisits({url: url}, processVisitsWithUrl(url));
numRequestsOutstanding++;
}
if (!numRequestsOutstanding) {
onAllVisitsProcessed();
}
});
// Maps URLs to a count of the number of times the user typed that URL into
// the omnibox.
// Callback for chrome.history.getVisits(). Counts the number of
// times a user visited a URL by typing the address.
var processVisits = function(url, visitItems) {
for (var i = 0, ie = visitItems.length; i < ie; ++i) {
// Ignore items unless the user typed the URL.
if (visitItems[i].transition != 'link') {
continue;
}
if (!urlToCount[url]) {
urlToCount[url].visits = 0;
urlToCount[url].exp = 0;
urlToCount[url].level = 0;
}
urlToCount[url].visits++;
}
// If this is the final outstanding call to processVisits(),
// then we have the final results. Use them to build the list
// of URLs to show in the popup.
if (!--numRequestsOutstanding) {
onAllVisitsProcessed();
}
};
// This function is called when we have the final list of URls to display.
var onAllVisitsProcessed = function() {
// Get the top scoring urls.
var level = 1; // User Level
var tolvl = level * 100 //Exp to 'level up'
urlArray = [];
for (var url in urlToCount) {
urlArray.push(url);
urlToCount[url].exp += (urlToCount[url].visits<=1)?10:5; // 5 if visited more than once
if (urlToCount[url].exp>=tolvl) urlToCount[url].level++;
}
// Sort the URLs by the number of times the user typed them.
urlArray.sort(function(a, b) {
return urlToCount[b].visits - urlToCount[a].visits;
});
buildPopupDom(divName, urlArray.slice(0, 10));
};
}
document.addEventListener('DOMContentLoaded', function () {
buildTypedUrlList("typedUrl_div");
});
相关文章:
- 将PHP变量传递给jQuery时遇到问题
- 参数变量出现ngTable指令问题
- 为变量分配多个nodejs导出返回值时出现问题
- 如何处理流星变量&对miniMongo有问题
- Jquery变量类型问题
- 带有.css()串联问题的jQuery变量
- JavaScript变量引用数组时出现问题
- 将一个php变量从js传递到php,并返回内容问题
- 简单的变量问题
- 可以't在quizz页面范围问题上用最终分数更新变量
- array_push平面数组问题-需要能够向数组中添加多个变量
- 尝试将变量传递到对象中时的作用域问题'的方法构造函数
- Javascript函数变量传递语法问题
- 访问带有变量的Object元素时出现问题
- 将 Ajax 数据分配给全局变量时出现问题
- JavaScript/jQuery中的基本数学问题-变量记忆值
- 简单的jQuery变量问题-变量冲突
- Javascript作用域问题:变量未被识别
- Javascript作用域问题-变量未定义
- 目标转换问题-变量修改