I'm trying to create an extension that will be able to remember some websites I visited. When I press the extension, there's a button (that allows the user to save the current URL of the tab and the title) and an “a” element, that will redirect to the URL saved. The idea is making it able to save different URLs, but first I want it able to save one.
Someone told me to do that with a Set(). I've the code, but something is wrong so when I press the “a” element with the last saved element, it opens that direction: chrome-extension://the_key_of_my_extension/[object%20Set%20Iterator]
or: chrome-extension://the_key_of_my_extension/[undefined]
.
What I'm doing wrong? I've been searching for a while but I don't see anything wrong. Thanks!
var urlSet = new Set();
var titleSet = new Set();
chrome.storage.sync.get(["activeTab", "nameOfTheTab"], function(items){
urlSet.add(items.activeTab);
titleSet.add(items.nameOfTheTabs);
document.getElementById('urlDude').href = [...urlSet];
document.getElementById('urlDude').innerHTML = [...titleSet];
});
saveItem.onclick = function(element) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
urlSet.add(tabs[0].url);
titleSet.add(tabs[0].title);
chrome.storage.sync.set({ "activeTab": [...urlSet] }, function(){ });
chrome.storage.sync.set({ "nameOfTheTab": [...titleSet] }, function(){ });
document.getElementById('urlDude').href = [...urlSet];
document.getElementById('urlDude').innerHTML = [...titleSet];
});
};
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="./popus.css">
</head>
<body>
<button id="saveItem">save</button>
<script src="popup.js"></script>
<br>
<br>
<a id="urlDude" target="_blank"><p>Hola</p></a>
<a id="urlDude2" target="_blank"><p>Hola2</p></a>
</body>
</html>
UPDATE:
This is what I have now:
var urlSet = new Map();
chrome.storage.sync.get(["activeTabs"], function(items){
if(!items.activeTabs)
return;
urlSet = new Map(JSON.parse(items.activeTabs));
AddLinks();
});
function AddLinks()
{
var myLinks="";
urlSet.forEach(function(value, key) {
//myLinks+="<a href='"+value+"' target='_blank'>"+key+"</a> <span>X</span><br>";
pushingData(value, key);
});
//document.getElementById('mydiv').innerHTML = myLinks;
}
saveItem.onclick = function(element) {
chrome.tabs.query({active: true}, function(tabs) {
urlSet.set(tabs[0].title, tabs[0].url);
chrome.storage.sync.set({ "activeTabs":JSON.stringify(Array.from(urlSet.entries())) });
pushingData(tabs[0].url, tabs[0].title);
});
};
function pushingData(value, key) {
var atags = document.createElement("a");
atags.href = value;
atags.innerHTML = key;
document.getElementById('mydiv').append(atags);
var newButton = document.createElement("button");
newButton.innerHTML = "x";
newButton.addEventListener('click', function() { urlSet.delete(key); });
document.getElementById('mydiv').append(newButton);
var breakLine = document.createElement("br");
document.getElementById('mydiv').append(breakLine);
};
I tried your code (with some slight modifications) in my extension and it worked, so I suspect there might be something stuck in the storage from your previous attempts. Here's what I've got:
var urlSet = new Map();
chrome.storage.sync.get(["activeTabs"], function(items){
if(!items.activeTabs)
return;
urlSet = new Map(JSON.parse(items.activeTabs));
AddLinks();
});
function AddLinks()
{
//There are better ways of doing this!
var myLinks="";
urlSet.forEach(function(value, key) {
myLinks+="<a href='"+value+"' target='_blank'>"+key+"</a>";
});
// You may want to come up with a way to delete links
//before they get out of control and proclaim independence
document.getElementById('mydiv').innerHTML = myLinks;
}
saveItem.onclick = function(element) {
chrome.tabs.query({active: true}, function(tabs) {
urlSet.set(tabs[0].title, tabs[0].url);
chrome.storage.sync.set({ "activeTabs":JSON.stringify(Array.from(urlSet.entries())) });
AddLinks();
});
};
UPDATE Seems like adding inline scripts to the popup is causing security issuer. While it is probably possible white-list your page to use its own script a better way of adding new elements is creating them with document.createElement, rather than generating a sting, to give you an idea of how it may look:
var delButton = document.createElement("button");
delButton.innerHTML = "delete";
delButton.addEventListener('click',function(){/*something useful happens here*/});
document.getElementById('mydiv').append(delButton);
Hey! Thanks for your answer! This worked for me. Just for finish, I've never used a Map. What is the syntax for deleting a specific item from the array? Thanks!
Glad to help! To delete use urlSet.delete('Title of the page you want to delete')
Me again. :( I try to delete an url of the list, but I get an error! I'm trying to make that, when I hover the mouse into the title of the url in the extension, it gets deleted. I added to every a tag the onmousehover event, and this linked to a function. But this isn't working, because I get
Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem:". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.
What's wrong? Thanks!I'm not sure how you're doing it, but how are you planning to click on URLs if hovering over them is deleting them? Why not to add a button next to the 'a' tag?
You're right, that would be a bad idea haha. It's better the button. But my trouble is that I can't execute inline code. I'm trying to make the delete button, but I can't. How can I get the title of the specific link I'm clicking?