0
我在嘗試修改Google Code 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
這正是我要找的,但它仍然沒有工作......是什麼urlToCount我需要使用?這似乎合乎邏輯。 我已經完成初學者和中級教程。儘管如此,我仍然犯了很多錯誤,對不起。 – Zeinir 2012-02-19 06:42:25
我不明白你的代碼 - 我沒有看到任何級別,我也沒有看到你在哪裏使用exp。按照 – mplungjan 2012-02-19 08:16:55
的方式,我將urlToCount更改爲urlToCount [url]嗯,它是一個簡單的腳本,用於獲取歷史數據並按訪問次數對其進行排序。 Var = exp和Var = level是我使用level和exp的地方,然後我使用了幾個document.witre將它打印到擴展的gui中。 – Zeinir 2012-02-19 14:29:45