我正在寻找一个可以计算我每天访问网站的数量的扩展程序,并且我已经从商店编辑了一个扩展程序,它可以完美地运行 但问题是,当我将该网站放入iframe时,它不再计算了,有人看看并在这里给我一些建议吗?非常感谢
的manifest.json
{
"background": {
"persistent": true,
"scripts": [ "jquery.js", "counter.js", "background.js" ]
},
"browser_action": {
"default_icon": "icon.png",
"default_popup": "counter.html"
},
"description": "Counter",
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgrQn46/0KFBVFH0l9mKUXPOns5h0YMQ0isRXkB/GzK1h8JfvILDuxkIC7jbyePzMgbY0b8TO7L7FR3m4qcnPJZemz2yrGnHOkppaOWaAuyJm2cQeOul89qNPsC1GROoKfBmajag5OaOr02y4de3MEKSTubtjlO5crbg8xKnp7nHbqkeIq9MOMsEo0/uOQMqvixMnIAfPamUPOQfqmG2lUeo9UgTUoFBaxpf5VhgcuDcLHnkkSrzRrL0DoYeYFqyXwGmiCdvmwkWF+Eax/+s2xGemqMSUj0mrMxzz8KfoWuAWn9pcXYMEINSelIwS/x+wf+NWWmsCY0pV9yF54+vprQIDAQAB",
"manifest_version": 2,
"name": "counter",
"permissions": [ "tabs", "history", "background", "webNavigation", "http://*/*", "activeTab" ],
"update_url": "https://clients2.google.com/service/update2/crx",
"version": "0.4"
}

counter.js
$(document).ready(function(){
// Display day count
$('.day-count').text(countForToday());
// Display yesterday count
var yesterdayToday = new Date();
var yesterday = new Date(yesterdayToday.setDate(yesterdayToday.getDate() - 1));
var yesterdayString = dateStringForDate(yesterday);
var yesterdayCount = countForDay(yesterdayString);
$('.yesterday-count').text(yesterdayCount);
// Display last 7 days
var weeklyTotal = countForDaysFromToday(7);
$('.week-count').text(weeklyTotal);
// Display last 28 days
var monthlyTotal = countForDaysFromToday(28);
$('.month-count').text(monthlyTotal);
// Total
var total = 0;
for (var i = 0; i < localStorage.length; i++){
var key = localStorage.key(i);
if (key.indexOf("dayCount") > -1) {
var entry = JSON.parse(localStorage.getItem(key));
total += entry.length;
}
}
$('.all-count').text(total);
chrome.browserAction.setBadgeBackgroundColor({ color: '#3BAFDA' });
});
function countForToday() {
// Display day count
var today = new Date();
var todayString = dateStringForDate(today);
var todayCount = countForDay(todayString);
return todayCount;
}
function appendUrlForDay(pageUrl, dateString) {
// get
var currentUrls = urlsForDay(dateString);
// append
var found = false;
for(var i = 0; i < currentUrls.length; i++) {
if (currentUrls[i] == pageUrl) {
found = true;
break;
}
}
if (!found) {
currentUrls.push(pageUrl);
}
// set
localStorage.setItem("dayCount-" + dateString, JSON.stringify(currentUrls));
}
function dateStringForDate(dateObj) {
var month = dateObj.getUTCMonth() + 1;
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var dateString = year + "/" + month + "/" + day;
return dateString;
}
function countForDaysFromToday(number_of_days) {
console.log(number_of_days);
var total = 0;
for (var i = 0; i < number_of_days; i++) {
var today = new Date();
var dayToLookup = new Date(today.setDate(today.getDate() - i));
var dayString = dateStringForDate(dayToLookup);
var dayCount = countForDay(dayString);
total += dayCount;
}
return total;
}
function urlsForDay(dateString) {
var item = localStorage.getItem("dayCount-" + dateString);
return JSON.parse(item);
}
function countForDay(dateString) {
var currentUrls = urlsForDay(dateString);
if (currentUrls != null) {
return currentUrls.length;
} else {
return 0;
}
}
&#13;
background.js
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
// Get chrome history
chrome.history.search({text: '', maxResults: 1}, function(data) {
data.forEach(function(page) {
var dateObj = new Date(page.lastVisitTime);
var dateString = dateStringForDate(dateObj);
// Display last page visit
var pageUrl = page.url;
// Set value to 0 on first load
if (localStorage.getItem("dayCount-" + dateString) === null) {
localStorage.setItem("dayCount-" + dateString, JSON.stringify([]));
}
// Check if last page contains mysite.com
if (pageUrl.indexOf("mysite.com") > -1) {
appendUrlForDay(pageUrl, dateString);
}
chrome.browserAction.setBadgeText({ text: countForToday().toString() });
});
});
});
&#13;
答案 0 :(得分:0)
默认情况下,您的内容脚本只会在页面的顶部框架中运行。如果您希望它为所有帧运行,则必须在清单中将all_frames设置为true。
Google在此处对此进行了记录:https://developer.chrome.com/extensions/content_scripts(只需在页面上搜索all_frames)。