Commit 1ca64510 authored by Christian's avatar Christian
Browse files

works with popup

parent ccfaf579
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);
if (msg.text === 'get_all_usernames') {
if (msg.text == 'get_all_usernames') {
sendResponse(findRedditUsernames());
}
});
if (msg.text == 'mark_members') {
console.log('!! mark_members !!');
if (msg.userlist) {
console.log('msg.userlist.length == ' + msg.userlist.length);
} else {
console.log('msg.userlist not defined');
}
markMemberUsers(msg.userlist);
sendResponse(true);
}
});
function findRedditUsernames() {
var d = document.all[0];
var all = document.all[0].querySelectorAll('div.content[role="main"] p.tagline a.author');
var li = [];
if (domContent) {
//domContent = domContent.replace('\n', ' ');
//domContent = domContent.replace('\r', ' ');
var matches = [];
for (var i=0; i<all.length; i++)
li.push(all[i].innerHTML);
// Use DOM transverse to find usernames -----
var all = document(domContent).querySelectorAll('.author');
return li;
}
for (var i=0; i<all.length; i++) {
matches.push(all[i].href);
}
function markMemberUsers(userlist) {
var s = userlist.join('<br>');
var elHead = document.getElementsByTagName('head')[0];
var elContent = document.all[0].querySelector('div.content[role="main"]');
var ico = 'icon.png'
// Use Regex to find usernames -----
// g -> global match
// m -> multiline
/*
var re = new RegExp('(?:\<a href\=\"https?\://www\.reddit\.com/user/)([a-zA-Z0-9_-]+)(?:/?\" )', 'gm');
var match = null;
while (match = re.exec(domContent)) {
if ( matches.indexOf(match[1]) < 0 )
matches.push(match[1]);
} */
if (matches) {
renderStatus('Regex raw username matches: ' + matches.length);
return matches;
} else {
renderStatus('Regex did not find any username matches.');
}
}
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>';
return [];
}
\ No newline at end of file
elContent.innerHTML = '';
for (var i=0; i<=userlist.length; i++) {
elContent.innerHTML += '<p><span class="reddmeet-icon"></span> ' + userlist[i] + '</p';
}
}
......@@ -5,20 +5,32 @@ Object.prototype.getName = function() {
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 currTab = null;
var currUsers = null;
document.addEventListener('DOMContentLoaded', function() {
renderStatus('Loading...');
getCurrentTabUrl(function(tab) {
if ( isRedditUrl(tab.url) ) {
renderStatus('This is Reddit...');
chrome.tabs.sendMessage(tab.id, {text: 'get_all_usernames'}, receiveUsernamesList);
}
else {
renderStatus('Some other website.');
currTab = tab;
if (currUsers === null) {
currCount += 1; renderStatus('['+currCount+']');
currUsers = []; // This prevents the userlist to be generated again.
chrome.tabs.sendMessage(currTab.id, {text: 'get_all_usernames'}, receiveUserlist);
}
renderStatus('Loading...');
};
});
});
......@@ -28,35 +40,28 @@ document.addEventListener('DOMContentLoaded', function() {
function isRedditUrl(url) {
var re = new RegExp("^https?://(\w+\.)+reddit.com(:\d+)?[$/]");
return url.match(re);
var re = new RegExp("^https?://(\w+\.)+reddit.com(:\d+)?[$/]");
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 receiveUsernamesList(usernamesList) {
// receives document.all[0] object from content.js
// domContent = domContent ? domContent : '';
renderStatus('DOM content with ' + domContent.length + ' chars... ');
var text = 'Still loading...';
if (domContent) {
var foundUser = findRedditUsernames(domContent);
rmUsers = queryReddmeetUsernames(foundUser);
text = 'FOUND: ';
for (var i=0; i<rmUsers.length && i<100; i++)
text += rmUsers[i] + ', ';
function receiveUserlist(userlist) {
if (!userlist) {
renderStatus('Did NOT get userlist :( ');
return;
}
renderStatus(text);
currUsers = queryReddmeetUsernames(unique_set(userlist));
chrome.tabs.sendMessage(currTab.id, {text: 'mark_members', 'userlist': userlist}, function(response){
//renderStatus(' !returned from mark_member_usernames! ');
});
}
function renderStatus(statusText) {
......@@ -69,6 +74,3 @@ function queryReddmeetUsernames(users) {
// ::: TODO :::
return users;
}
/* chrome.browserAction.onClicked.addListener(function (tab) { }); */
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