Skip to content

Instantly share code, notes, and snippets.

@Utopiah
Created March 13, 2021 20:20
Show Gist options
  • Save Utopiah/19e7d711a08fbabf7c91ea695cd48b50 to your computer and use it in GitHub Desktop.
Save Utopiah/19e7d711a08fbabf7c91ea695cd48b50 to your computer and use it in GitHub Desktop.
WebExtension main JS to add current URL to PIM (PmWiki POST API)
// Based on https://github.com/mdn/webextensions-examples/tree/master/bookmark-it
/*
* Add the bookmark on the current page.
*/
function addToBucket() {
fetch('https://fabien.benetou.fr/PIMVRdata/URLBucketTest?action=source')
.then( response => { return response.text() } )
.then( data => {
var writeURL = 'https://fabien.benetou.fr/PIMVRdata/URLBucketTest?action=edit';
fetch(writeURL, {
method: 'POST',
headers: {'Content-Type':'application/x-www-form-urlencoded'},
body: "post=1&author=PIMVR&authpw=mypassword&text="+data+'%0a'+currentTab.url
}).then(res => res).then(res => console.log(res))
})
}
browser.browserAction.onClicked.addListener(addToBucket);
/*
* Switches currentTab to reflect the currently active tab
*/
function updateActiveTab(tabs) {
function isSupportedProtocol(urlString) {
var supportedProtocols = ["https:", "http:", ];
var url = document.createElement('a');
url.href = urlString;
return supportedProtocols.indexOf(url.protocol) != -1;
}
function updateTab(tabs) {
if (tabs[0]) {
currentTab = tabs[0];
}
}
var gettingActiveTab = browser.tabs.query({active: true, currentWindow: true});
gettingActiveTab.then(updateTab);
}
// listen to tab URL changes
browser.tabs.onUpdated.addListener(updateActiveTab);
// listen to tab switching
browser.tabs.onActivated.addListener(updateActiveTab);
// listen for window switching
browser.windows.onFocusChanged.addListener(updateActiveTab);
// update when the extension loads initially
updateActiveTab();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment