Commit cc945f93 authored by Christian's avatar Christian
Browse files

content.js via messages

parent 1ca64510
Object.prototype.getName = function() {
var funcNameRegex = /function (.{1,})\(/;
var results = (funcNameRegex).exec((this).constructor.toString());
return (results && results.length > 1) ? results[1] : "";
};
// http://stackoverflow.com/a/13486540/5520354
function unique_set(arr) {
return arr.filter(function (e, i, arr) {
return arr.lastIndexOf(e) === i;
});
}
///////////////////////////////////////////////////////////////////////
var currCount = 0;
var currUsers = null;
console.log('--> background.js: This is the background page, yo!');
chrome.webNavigation.onDOMContentLoaded.addListener(function(tabDetails) {
// details.tabId The ID of the tab in which the navigation occurs.
// details.url
// details.processId The ID of the process runs the renderer for this tab.
// details.frameId 0 indicates the navigation happens in the tab content window;
// a positive value indicates navigation in a subframe.
// Frame IDs are unique within a tab.
// details.timeStamp The time when the page's DOM was fully constructed,
// in milliseconds since the epoch.
if (tabDetails.frameId > 0) return; // Only watch tab level frames.
console.log('--> background.js: webNavigation.onCompleted at: ' + tabDetails.url);
if (isRedditUrl(tabDetails.url)) {
currCount += 1;
console.log('--> background.js: Hey, tab isRedditUrl, yo! Counter: ' + currCount);
console.log('--> background.js: ', tabDetails);
chrome.tabs.sendMessage(tabDetails.tabId, {text: 'harvest_usernames'}, function(userlist) {
if (userlist) {
console.log('--> background.js: SUCCESS: harvested userlist for tabId "' + tabDetails.tabId + '" ');
receiveUserlist(tabDetails, userlist);
} else {
console.log('--> background.js: ERROR: could not harvest userlist for tabId "' + tabDetails.tabId + '" ');
return;
}
});
};
});
///////////////////////////////////////////////////////////////////////
function isRedditUrl(url) {
var re = new RegExp("^https\://www\.reddit\.com/r/");
return url.match(re);
}
//function getCurrentTabUrl(callback) {
// var queryInfo = { active: true, currentWindow: true, };
// chrome.tabs.query(queryInfo, function(tabs) {
// var tab = tabs[0];
// callback(tab);
// });
//}
function receiveUserlist(tabDetails, userlist) {
console.log('--> background.js: raw userlist.length: ' + userlist.length);
userlist = unique_set(userlist);
console.log('--> background.js: unique userlist.length: ' + userlist.length);
userlist = queryReddmeetUsernames(userlist);
console.log('--> background.js: members userlist length: ' + userlist.length);
chrome.tabs.sendMessage(tabDetails.tabId, {text: 'mark_members', 'userlist': userlist}, function(response){
console.log('--> background.js: Return from "mark_members" message: ' + response);
});
}
function renderStatus(statusText) {
//document.getElementById('status').textContent += '\n' + statusText;
}
function queryReddmeetUsernames(users) {
// For a list of usernames, query the reddmeet API if they have an account.
// The API returns a list of usernames that do have a reddmeet profile.
// ::: TODO :::
return users;
}
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
console.log('!! chrome.runtime.onMessage.addListener !!');
console.log('ID -- 0002');
console.log('msg.text: ' + msg.text);
console.log(msg);
console.log('--> content.js: chrome.runtime.onMessage.addListener');
console.log('--> content.js: msg.text: ' + msg.text);
debugger;
if (msg.text == 'get_all_usernames') {
sendResponse(findRedditUsernames());
if (msg.text == 'harvest_usernames') {
console.log('--> content.js: msg.text == "harvest_usernames"');
sendResponse(['user1', 'user2', 'user3']);
//sendResponse(findRedditUsernames());
}
if (msg.text == 'mark_members') {
console.log('!! mark_members !!');
console.log('--> content.js: msg.text == "mark_members"');
if (msg.userlist) {
console.log('msg.userlist.length == ' + msg.userlist.length);
console.log('--> content.js: msg.userlist.length == ' + msg.userlist.length);
} else {
console.log('msg.userlist not defined');
console.log('--> content.js: msg.userlist not defined');
}
markMemberUsers(msg.userlist);
......@@ -41,7 +44,7 @@ function markMemberUsers(userlist) {
elHead.innerHTML += '<style>.reddmeet-icon { display: block; overflow: hidden; width: 1em; height: 1em; background: transparent url(chrome-extension://__MSG_@@extension_id__/icon12.png) center center no-repeat; background-size: cover; border: 0; margin: 0; padding: 0; }</style>';
elContent.innerHTML = '';
for (var i=0; i<=userlist.length; i++) {
for (var i=0; i<userlist.length; i++) {
elContent.innerHTML += '<p><span class="reddmeet-icon"></span> ' + userlist[i] + '</p';
}
}
......@@ -5,9 +5,13 @@
"description": "Adds a link to usernames with a reddmeet profile.",
"version": "0.1",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
"background": {
"persistent": true,
"scripts": ["background.js", "content.js"]
},
"page_action": {
"default_icon": "icon.png"
},
"content_scripts": [{
......@@ -17,6 +21,8 @@
"permissions": [
"activeTab",
"webNavigation",
"tabs",
"https://reddmeet.com/"
]
}
XXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXX
Object.prototype.getName = function() {
var funcNameRegex = /function (.{1,})\(/;
var results = (funcNameRegex).exec((this).constructor.toString());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment