My goal is to create a button that when clicked deletes the parent divs on reddit whose links have already been visited. Due to security issues, visited link status cannot be detected from unprivileged javascript, thus, I attempted a chrome extension. However, this doesn’t seem to work either, as even the javascript called from an extension with history permission returns “Uncaught TypeError: Cannot call method ‘search’ of undefined.” Thus, I am still looking for a way to do this.
Update 1/8/2013
I’m very close to getting this working, however, I can’t get n_results in the content script to accurately reflect its status in the background script. Any ideas? Latest code below.
Update 1/8/2013
It’s working! See below if you need to do something like described above.
manifest.json
{
"name": "Never The Same (NTS) Reddit",
"version": "1.0",
"manifest_version": 2,
"description": "Previously visited links are deleted.",
"browser_action": {"default_icon": "icon.png"},
"permissions": ["history","tabs"],
"content_scripts": [
{
"run_at": "document_end",
"matches": ["http://www.reddit.com/*"],
"js": ["jquery-1.8.3.min.js", "ntsreddit_content.js"]
}
],
"background": {"scripts": ["ntsreddit_background.js"]}
}
ntsreddit_background.js
chrome.extension.onMessage.addListener(
function (request, sender) {
chrome.history.getVisits({"url": request.url},
function (visits) {
if (visits.length > 0) {
chrome.tabs.sendMessage(sender.tab.id, {
"url": request.url
});
};
});
});
ntsreddit_content.js
$("div.thing a.title").each(
function(index, value) {
chrome.extension.sendMessage({"url": value.href});
});
chrome.extension.onMessage.addListener(function (message) {
anchor=$('a[href^="' + message.url + '"][class~="title"]')
if (anchor.length > 0) {
anchor.closest(".thing").remove();
};
});
Content scripts have some limitations. They cannot use chrome.* APIs (except for parts of chrome.extension), your code
chrome.historyin bookmarklet.js will always beundefinedEDIT:
After making some changes to your script i got it running.
Changes
ntsreddit_background.js
Moved
tabs.sendMessage()to handle asynchronous nature of chrome API(s) and eliminated deprecated API(s)ntsreddit_content.js
Added a listener to handle response received from background Page
I hope this solves your problem.