condividi-link-fediverso-fi.../js/index.js

261 lines
7.6 KiB
JavaScript

/*
Original by yuvaraj
Copyright (c) 2016
Forked by Mickie
Copyleft (ɔ) 2019
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
;
(function () {
'use strict';
// Send Message To Background Script
function sendMessage(tab) {
const { id } = tab;
browser.runtime.sendMessage({
type: 'sharer-tab-id',
data: { id }
});
}
/*
* Assign URLs To Respective Element
*/
function urlAssigner(btn = {}) {
const { id, href } = btn;
const a = document.getElementById(id);
a.href = href;
a.target = '_blank';
a.addEventListener('click', event => {
console.log(id);
if(id == 'url-nextcloud') {
event.preventDefault();
sendNextcloudBookmark();
}
//window.setTimeout(() => window.close(), 10)
});
}
function sendNextcloudBookmark(tabUrl) {
$.support.cors = true;
console.log('Sending...');
var server = $('#url-nextcloud').attr('data-server');
var username = $('#url-nextcloud').attr('data-username');
var password = $('#url-nextcloud').attr('data-password');
var endpoint = server + '/index.php/apps/bookmarks/public/rest/v2/bookmark';
var tabUrL = $('#url-nextcloud').attr('data-url');
var tabTitle = $('#url-nextcloud').attr('data-title');
console.log(server);
console.log(username,password);
console.log(tabUrL,tabTitle);
$.ajax({
url: endpoint,
method: "POST",
//basic authentication
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
},
data: {
url: tabUrL,
title: tabTitle,
description: '',
is_public: true
},
dataType: 'json',
})
.success(function(result) {
console.log(result);
$('.alerta').html('<div class="alert alert-info">Successfully bookmarked!</div>');
})
.error(function(XMLHttpRequest, status, errorThrown){
$('.alerta').html('<div class="alert alert-danger">Error, something was wrong!</div>');
console.log('ajax error');
console.log("Status: " + status);
console.log("Error: " + errorThrown);
});
}
/*
* Make Social Button Objects
*/
function mkBtns(tabUrl = '', tabTitle = '') {
// Set hosts
var gnusocialItem = browser.storage.sync.get('gnusocialHost');
gnusocialItem.then((res) => {
document.querySelector("#url-gnusocial").href = [res.gnusocialHost || `https://gnusocial.no`] + `/?action=newnotice&status_textarea=${tabTitle} ${tabUrl}`;
});
var mastodonItem = browser.storage.sync.get('mastodonHost');
mastodonItem.then((res) => {
document.querySelector("#url-mastodon").href = [res.mastodonHost || `https://mastodon.social`] + `/share?text=${tabTitle}&url=${tabUrl}`;
});
var hubzillaItem = browser.storage.sync.get('hubzillaHost');
hubzillaItem.then((res) => {
document.querySelector("#url-hubzilla").href = [res.hubzillaHost || `https://start.hubzilla.org`] + `/rpost?body=${tabTitle} &url=${tabUrl}`;
});
var diasporaItem = browser.storage.sync.get('diasporaHost');
diasporaItem.then((res) => {
document.querySelector("#url-diaspora").href = [res.diasporaHost || `https://diasp.eu`] + `/bookmarklet?url=${tabUrl}&title=${tabTitle}&jump-doclose`;
});
var friendicaItem = browser.storage.sync.get('friendicaHost');
friendicaItem.then((res) => {
document.querySelector("#url-friendica").href = [res.friendicaHost || `https://libranet.de`] + `/bookmarklet?url=${tabUrl}&title=${tabTitle}&jump-doclose`;
});
var socialhomeItem = browser.storage.sync.get('socialhomeHost');
socialhomeItem.then((res) => {
document.querySelector("#url-socialhome").href = [res.socialhomeHost || `https://socialhome.network`] + `/bookmarklet?url=${tabUrl}&title=${tabTitle}&jump-doclose`;
});
var lemmyItem = browser.storage.sync.get('lemmyHost');
lemmyItem.then((res) => {
document.querySelector("#url-lemmy").href = [res.lemmyHost || `https://dev.lemmy.ml`] + `/create_post?url=${tabUrl}&title=${tabTitle}`;
});
var pleromaItem = browser.storage.sync.get('pleromaHost');
pleromaItem.then((res) => {
document.querySelector("#url-pleroma").href = [res.pleromaHost || `https://pleroma-social.ml`] + `/share?message=${tabTitle} ${tabUrl}`;
});
var xmppItem = browser.storage.sync.get('xmppHost');
xmppItem.then((res) => {
document.querySelector("#url-xmpp").href = `xmpp:` + [res.xmppHost] + `?message=${tabTitle} ${tabUrl}`;
});
var nextcloudHost = browser.storage.sync.get('nextcloudHost');
var nextcloudUsername = browser.storage.sync.get('nextcloudUsername');
var nextcloudPassword = browser.storage.sync.get('nextcloudPassword');
nextcloudHost.then((res) => {
document.querySelector("#url-nextcloud").href = [res.nextcloudHost];
document.querySelector("#url-nextcloud").dataset.server = [res.nextcloudHost];
document.querySelector("#url-nextcloud").dataset.url = tabUrl;
document.querySelector("#url-nextcloud").dataset.title = tabTitle;
});
nextcloudUsername.then((res) => {
document.querySelector("#url-nextcloud").dataset.username = [res.nextcloudUsername];
});
nextcloudPassword.then((res) => {
document.querySelector("#url-nextcloud").dataset.password = [res.nextcloudPassword];
});
// Set social Buttons
const socialBtns = [];
const gnusocial = {};
gnusocial.href = gnusocialItem;
gnusocial.id = 'url-gnusocial';
socialBtns.push(gnusocial);
const mastodon = {};
mastodon.href = mastodonItem;
mastodon.id = 'url-mastodon';
socialBtns.push(mastodon);
const hubzilla = {};
hubzilla.href = hubzillaItem;
hubzilla.id = 'url-hubzilla';
socialBtns.push(hubzilla);
const diaspora = {};
diaspora.href = diasporaItem;
diaspora.id = 'url-diaspora';
socialBtns.push(diaspora);
const friendica = {};
friendica.href = friendicaItem;
friendica.id = 'url-friendica';
socialBtns.push(friendica);
const socialhome = {};
socialhome.href = socialhomeItem;
socialhome.id = 'url-socialhome';
socialBtns.push(socialhome);
const lemmy = {};
lemmy.href = lemmyItem;
lemmy.id = 'url-lemmy';
socialBtns.push(lemmy);
const pleroma = {};
pleroma.href = pleromaItem;
pleroma.id = 'url-pleroma';
socialBtns.push(pleroma);
const xmpp = {};
xmpp.href = xmppItem;
xmpp.id = 'url-xmpp';
socialBtns.push(xmpp);
const nextcloud = {};
nextcloud.href = nextcloudHost;
nextcloud.id = 'url-nextcloud';
socialBtns.push(nextcloud);
socialBtns.forEach(urlAssigner);
}
;
(function getCurrentTabUrl() {
const queryInfo = {
active: true,
currentWindow: true
};
chrome.tabs.query(queryInfo, (tabs = []) => {
if (tabs.length === 0) {
return;
}
const tab = (tabs[0] || {});
const tabUrL = encodeURIComponent(tab.url);
const tabTitle = encodeURIComponent(tab.title);
mkBtns(tabUrL, tabTitle);
});
// End
})();
})();
/*
* Make Shortcut
*/
let gettingAllCommands = browser.commands.getAll();
gettingAllCommands.then((commands) => {
for (let command of commands) {
console.log(command);
}
});
browser.commands.onCommand.addListener(function(command) {
if (command == "_execute_browser_action") {
// console.log("execute the feature!");
}
});