Added password for scuttle API. Fixed indentation. Added stub menu actions. Started working on bookmarking with scuttle API.

This commit is contained in:
yohan 2019-09-06 20:20:49 +02:00
parent 3a35cbfda4
commit d6ee98a263
4 changed files with 198 additions and 90 deletions

View File

@ -1,102 +1,206 @@
function onError(error) { function onError(error) {
console.log(`Error: ${error}`); console.log(`Error: ${error}`);
}
function onRemoved() {
console.log('Removed');
}
function onShared_remove(tab) {
console.log('Shared');
//var removing = tab.remove();
//removing.then(onRemoved, onError);
} }
function shareURL(selectionContent,currentTab){ function shareURL(selectionContent,currentTab){
browser.storage.local.get(["instance_url","window_width","window_height","remove_querystrings","exceptUrlList"],function(item){ browser.storage.local.get(["instance_url","window_width","window_height","remove_querystrings","exceptUrlList"],function(item){
instance = item["instance_url"];
windowWidth = item["window_width"];
windowHeight = item["window_height"];
noQueryStrings = item["remove_querystrings"];
exceptUrlList = item["exceptUrlList"];
instance = item["instance_url"]; // manages Mozilla Firefox reader mode
windowWidth = item["window_width"]; var rawUrl = currentTab.url;
windowHeight = item["window_height"]; var partToRemove = "about:reader?url=";
noQueryStrings = item["remove_querystrings"]; if(rawUrl.includes(partToRemove)) {
exceptUrlList = item["exceptUrlList"]; rawUrl = rawUrl.substring(partToRemove.length);
rawUrl = decodeURIComponent(rawUrl);
// manages Mozilla Firefox reader mode
var rawUrl = currentTab.url;
var partToRemove = "about:reader?url=";
if(rawUrl.includes(partToRemove)) {
rawUrl = rawUrl.substring(partToRemove.length);
rawUrl = decodeURIComponent(rawUrl);
}
// manages URL query strings
if (noQueryStrings == true) {
var flagRemove = true;
var urlList = exceptUrlList.split(/,\s*/);
urlList.forEach(function(baseUrl) {
if (rawUrl.startsWith(baseUrl)) {
flagRemove = false
} }
});
if (flagRemove) {rawUrl = rawUrl.split("?")[0];}
}
var url = instance + "/bookmarks.php?action=add&address=" + encodeURIComponent(rawUrl) + "&title=" + encodeURIComponent(currentTab.title) + "&description=" + encodeURIComponent(selectionContent); // manages URL query strings
widthInt = Number(windowWidth); if (noQueryStrings == true) {
heightInt = Number(windowHeight); var flagRemove = true;
var urlList = exceptUrlList.split(/,\s*/);
urlList.forEach(function(baseUrl) {
if (rawUrl.startsWith(baseUrl)) {
flagRemove = false
}
});
if (flagRemove) {rawUrl = rawUrl.split("?")[0];}
}
browser.windows.create({ var url = instance + "/bookmarks.php?action=add&address=" + encodeURIComponent(rawUrl) + "&title=" + encodeURIComponent(currentTab.title) + "&description=" + encodeURIComponent(selectionContent);
url: url, widthInt = Number(windowWidth);
width: widthInt, heightInt = Number(windowHeight);
height: heightInt,
type: "popup" browser.windows.create({
},(win)=>{ url: url,
browser.tabs.onUpdated.addListener((tabId,changeInfo) =>{ width: widthInt,
if(tabId === win.tabs[0].id){ height: heightInt,
if(changeInfo.url){ type: "popup"
var new_url },(win)=>{
new_url = changeInfo.url browser.tabs.onUpdated.addListener((tabId,changeInfo) =>{
if((new_url.includes("action=add") == false) && (new_url.includes("edit.php") == false)){ if(tabId === win.tabs[0].id){
browser.windows.remove(win.id); if(changeInfo.url){
} var new_url
} new_url = changeInfo.url
} if((new_url.includes("action=add") == false) && (new_url.includes("edit.php") == false)){
}); browser.windows.remove(win.id);
}); }
}); }
}
});
});
});
}
function shareURL_API(selectionContent,currentTab){
return new Promise((resolve, reject) => {
browser.storage.local.get(["instance_url","username","password","remove_querystrings","exceptUrlList"],function(item){
instance = item["instance_url"];
// Build formData object.
let formData = new FormData();
formData.append('name', 'John');
formData.append('password', 'John123');
fetch(instance+'/api/posts_add.php', { method: 'POST', body: formData })
.then(function (response) {
if (response.ok) {
return response.json();
} else {
return Promise.reject({
status: response.status,
statusText: response.statusText
});
}
})
.then(function (data) {
console.log('success', data);
resolve("OK");
})
.catch(function (error) {
console.log('error', error);
reject("KO");
});
});
})
} }
browser.contextMenus.create({ browser.contextMenus.create({
id: "semantic-scuttle", id: "semantic-scuttle",
title: "Add to (Semantic)Scuttle", title: "Add to (Semantic)Scuttle",
onclick: function(){ onclick: function(){
browser.tabs.query({ currentWindow: true, active: true }, function(tabs) { browser.tabs.query({ currentWindow: true, active: true }, function(tabs) {
tab = tabs[0]; tab = tabs[0];
if((tab.url.includes("about:reader?url=") == true) || (tab.url.includes("https://addons.mozilla.org/") == true)){ if((tab.url.includes("about:reader?url=") == true) || (tab.url.includes("https://addons.mozilla.org/") == true)){
shareURL("",tab); shareURL("",tab);
} }
else else
{ {
browser.tabs.sendMessage(tab.id, {method: "getSelection"}).then(response => { browser.tabs.sendMessage(tab.id, {method: "getSelection"}).then(response => {
shareURL(response.response,tab); shareURL(response.response,tab);
}).catch(onError); }).catch(onError);
} }
}); });
}, },
contexts: ["all"] contexts: ["all"]
}); });
browser.contextMenus.create({ browser.contextMenus.create({
id: "my-scuttle", id: "scuttle-session-save",
title: "My (Semantic)Scuttle", title: "Save and close all",
onclick: function(){ onclick: function(){
browser.storage.local.get(["instance_url","username"],function(item){ browser.tabs.query({}, function(tabs) {
myurl = item["instance_url"] + "/bookmarks.php/" + item["username"]; for (let tab of tabs) {
var creating = browser.tabs.create({url: myurl}); if((tab.url.includes("about:reader?url=") == true) || (tab.url.includes("https://addons.mozilla.org/") == true)){
}) shareURL_API("",tab).then(onShared_remove, onError);
}, }
contexts: ["all"] else
{
browser.tabs.sendMessage(tab.id, {method: "getSelection"})
.then(response => shareURL_API(response.response,tab))
.then(onShared_remove)
.catch(onError);
}
}
});
},
contexts: ["all"]
});
browser.contextMenus.create({
id: "scuttle-session-restore",
title: "Select session to restore",
onclick: function(){
browser.tabs.query({ currentWindow: true, active: true }, function(tabs) {
tab = tabs[0];
if((tab.url.includes("about:reader?url=") == true) || (tab.url.includes("https://addons.mozilla.org/") == true)){
shareURL("",tab);
}
else
{
browser.tabs.sendMessage(tab.id, {method: "getSelection"}).then(response => {
shareURL(response.response,tab);
}).catch(onError);
}
});
},
contexts: ["all"]
});
browser.contextMenus.create({
id: "scuttle-selector",
title: "Select urls to add",
onclick: function(){
browser.tabs.query({ currentWindow: true, active: true }, function(tabs) {
tab = tabs[0];
if((tab.url.includes("about:reader?url=") == true) || (tab.url.includes("https://addons.mozilla.org/") == true)){
shareURL("",tab);
}
else
{
browser.tabs.sendMessage(tab.id, {method: "getSelection"}).then(response => {
shareURL(response.response,tab);
}).catch(onError);
}
});
},
contexts: ["all"]
});
browser.contextMenus.create({
id: "my-scuttle",
title: "My (Semantic)Scuttle",
onclick: function(){
browser.storage.local.get(["instance_url","username"],function(item){
myurl = item["instance_url"] + "/bookmarks.php/" + item["username"];
var creating = browser.tabs.create({url: myurl});
})
},
contexts: ["all"]
}); });
browser.browserAction.onClicked.addListener((tab) => { browser.browserAction.onClicked.addListener((tab) => {
if((tab.url.includes("about:reader?url=") == true) || (tab.url.includes("https://addons.mozilla.org/") == true)){ if((tab.url.includes("about:reader?url=") == true) || (tab.url.includes("https://addons.mozilla.org/") == true)){
shareURL("",tab); shareURL("",tab);
} }
else else
{ {
browser.tabs.sendMessage(tab.id, {method: "getSelection"}).then(response => { browser.tabs.sendMessage(tab.id, {method: "getSelection"}).then(response => {
shareURL(response.response,tab); shareURL(response.response,tab);
}).catch(onError); }).catch(onError);
} }
}); });

View File

@ -8,14 +8,14 @@
"48" : "data/icon48.png" "48" : "data/icon48.png"
}, },
"applications": { "applications": {
"gecko": { "gecko": {
"id": "add2scuttle@spaceoasis.gotdns.org", "id": "add2scuttle@spaceoasis.gotdns.org",
"strict_min_version": "52.0" "strict_min_version": "52.0"
} }
}, },
"options_ui" : { "options_ui" : {
"page": "options.html" "page": "options.html"
}, },
"permissions" : [ "permissions" : [
"storage", "storage",
"notifications", "notifications",

View File

@ -10,9 +10,11 @@
<form> <form>
<label>(Semantic)Scuttle instance URL <input type="text" id="instance_url" ></label><br /> <label>(Semantic)Scuttle instance URL <input type="text" id="instance_url" ></label><br />
<label>(Semantic)Scuttle username <input type="text" id="username" ></label><br /> <label>(Semantic)Scuttle username <input type="text" id="username" ></label><br />
<label>(Semantic)Scuttle password <input type="text" id="password" ></label><br />
<label>Window Width <input type="text" id="window_width" ></label><br /> <label>Window Width <input type="text" id="window_width" ></label><br />
<label>Window Height <input type="text" id="window_height" ></label><br /> <label>Window Height <input type="text" id="window_height" ></label><br />
<label>Remove querystrings from URL <input type="checkbox" id="remove_querystrings" ></label><label> Except for (separated by commas) <input type="text" id="exceptUrlList"></label><br /> <label>Remove querystrings from URL <input type="checkbox" id="remove_querystrings" ></label><br />
<label> Except for (separated by commas) <input type="text" id="exceptUrlList"></label><br />
<button type="submit">Save</button> <button type="submit">Save</button>
</form> </form>

View File

@ -3,6 +3,7 @@ function saveOptions(e) {
browser.storage.local.set({ browser.storage.local.set({
instance_url: document.querySelector("#instance_url").value , instance_url: document.querySelector("#instance_url").value ,
username: document.querySelector("#username").value , username: document.querySelector("#username").value ,
password: document.querySelector("#password").value ,
window_width: document.querySelector("#window_width").value , window_width: document.querySelector("#window_width").value ,
window_height: document.querySelector("#window_height").value, window_height: document.querySelector("#window_height").value,
remove_querystrings: document.querySelector("#remove_querystrings").checked, remove_querystrings: document.querySelector("#remove_querystrings").checked,
@ -15,6 +16,7 @@ function restoreOptions() {
function setCurrentChoices(result) { function setCurrentChoices(result) {
document.querySelector("#instance_url").value = result["instance_url"] || "http://semanticscuttle.sourceforge.net/"; document.querySelector("#instance_url").value = result["instance_url"] || "http://semanticscuttle.sourceforge.net/";
document.querySelector("#username").value = result["username"] || "Me"; document.querySelector("#username").value = result["username"] || "Me";
document.querySelector("#password").value = result["password"] || "Password";
document.querySelector("#window_width").value = result["window_width"] || "640"; document.querySelector("#window_width").value = result["window_width"] || "640";
document.querySelector("#window_height").value = result["window_height"] || "480"; document.querySelector("#window_height").value = result["window_height"] || "480";
document.querySelector("#remove_querystrings").checked = result["remove_querystrings"] || false; document.querySelector("#remove_querystrings").checked = result["remove_querystrings"] || false;