How to use the webextension-polyfill.sessions function in webextension-polyfill

To help you get started, we’ve selected a few webextension-polyfill examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lusakasa / saka / src / suggestion_engine / server / providers / closedTab.js View on Github external
export async function recentlyClosedTabSuggestions() {
  const { recentlyClosed } = await browser.runtime.getBackgroundPage();
  const sessions = await browser.sessions.getRecentlyClosed();
  const filteredSessions = [];

  // TODO: This for loop is currently flagged by the airbnb eslint rules.
  // See: https://github.com/airbnb/javascript/issues/1271
  // Not disabling the rule as this might be fixable in the future using filter.
  // This for loop is needed at the moment as a workaround since filter does not support async.
  for (const session of sessions) {
    if (session.tab && !(await isSakaUrl(session.tab.url))) {
      filteredSessions.push(session);
    }
  }

  return filteredSessions
    .map(session => {
      const foundTab = recentlyClosed.findIndex(tab => {
        return tab.tabId === session.tab.id;
github lusakasa / saka / src / suggestion_engine / server / index.js View on Github external
export async function activateSuggestion(suggestion) {
  switch (suggestion.type) {
    case 'tab':
      await browser.tabs.update(suggestion.tabId, { active: true });
      await browser.windows.update(suggestion.windowId, { focused: true });
      break;
    case 'closedTab':
      await browser.sessions.restore(suggestion.sessionId);
      break;
    case 'bookmark':
      await focusOrCreateTab(suggestion.url);
      break;
    case 'history':
      await focusOrCreateTab(suggestion.url);
      break;
    case 'recentlyViewed':
      await activateSuggestion({
        ...suggestion,
        type: suggestion.originalType
      });
      break;
    default:
      console.error(
        `activation not yet implemented for suggestions of type ${
github kumabook / bebop / src / utils / sessions.js View on Github external
import browser from 'webextension-polyfill';
import { getFaviconUrl } from './url';

let MAX_SESSION_RESULTS = 20;
if (browser.sessions) {
  ({ MAX_SESSION_RESULTS } = browser.sessions);
}

export function session2candidate(session) {
  const { tab, window } = session;
  if (tab) {
    return {
      id:         `session-tab-${tab.sessionId}`,
      label:      `${tab.title}:${tab.url}`,
      type:       'session',
      args:       [tab.sessionId, 'tab', tab.windowId],
      faviconUrl: getFaviconUrl(tab.url),
    };
  }
  const t = window.tabs[0];
  const title = `${t.title} + ${window.tabs.length - 1} tabs`;
  return {
github kumabook / bebop / src / actions.js View on Github external
{ id: 'delete-bookmark'            , label: 'delete bookmark(s)'           , icon: 'delete' , handler: deleteBookmark         , contentHandler: noop },
];

const hatebuActions = [
  { id: 'open-url'                   , label: 'open'                         , icon: 'open'   , handler: openUrl                , contentHandler: noop },
  { id: 'open-urls-in-new-tab'       , label: 'open url(s) in new tab(s)'    , icon: 'tab'    , handler: openUrlsInNewTab       , contentHandler: noop },
  { id: 'open-urls-in-new-window'    , label: 'open url(s) in new window'    , icon: 'window' , handler: openUrlsInNewWindow    , contentHandler: noop },
  { id: 'open-urls-in-private-window', label: 'open url(s) in private window', icon: 'private', handler: openUrlsInPrivateWindow, contentHandler: noop },
  { id: 'edit-bookmark'              , label: 'edit bookmark(s)'             , icon: 'open'   , handler: openHatebuEntryPage    , contentHandler: noop },
];

const sessionActions = [
  { id: 'restore-session', label: 'restore-session', icon: 'session', handler: restoreSession },
];

if (browser.sessions && browser.sessions.forgetClosedTab) {
  sessionActions.push({ id: 'forget-session' , label: 'forget-session' , icon: 'session', handler: forgetSession });
}

const cursorActions = [
  { id: 'forward-char'        , label: 'Forward char'        , icon: null, handler: noop, contentHandler: cursor.forwardChar },
  { id: 'backward-char'       , label: 'Backward char'       , icon: null, handler: noop, contentHandler: cursor.backwardChar },
  { id: 'beginning-of-line'   , label: 'Beginning of line'   , icon: null, handler: noop, contentHandler: cursor.beginningOfLine },
  { id: 'end-of-line'         , label: 'End of line'         , icon: null, handler: noop, contentHandler: cursor.endOfLine },
  { id: 'next-line'           , label: 'Next line'           , icon: null, handler: noop, contentHandler: cursor.nextLine },
  { id: 'previous-line'       , label: 'Previous line'       , icon: null, handler: noop, contentHandler: cursor.previousLine },
  { id: 'end-of-buffer'       , label: 'End of buffer'       , icon: null, handler: noop, contentHandler: cursor.endOfBuffer },
  { id: 'beginning-of-buffer' , label: 'Beginning of buffer' , icon: null, handler: noop, contentHandler: cursor.beginningOfBuffer },
  { id: 'delete-backward-char', label: 'Delete backward char', icon: null, handler: noop, contentHandler: cursor.deleteBackwardChar },
  { id: 'kill-line'           , label: 'Kill line'           , icon: null, handler: noop, contentHandler: cursor.killLine },
];
github kumabook / bebop / src / utils / sessions.js View on Github external
import browser from 'webextension-polyfill';
import { getFaviconUrl } from './url';

let MAX_SESSION_RESULTS = 20;
if (browser.sessions) {
  ({ MAX_SESSION_RESULTS } = browser.sessions);
}

export function session2candidate(session) {
  const { tab, window } = session;
  if (tab) {
    return {
      id:         `session-tab-${tab.sessionId}`,
      label:      `${tab.title}:${tab.url}`,
      type:       'session',
      args:       [tab.sessionId, 'tab', tab.windowId],
      faviconUrl: getFaviconUrl(tab.url),
    };
  }
  const t = window.tabs[0];
  const title = `${t.title} + ${window.tabs.length - 1} tabs`;
github kumabook / bebop / src / utils / sessions.js View on Github external
return Promise.all(candidates.map((candidate) => {
    const sessionId = candidate.args[0];
    if (candidate.args[1] === 'tab') {
      return browser.sessions.forgetClosedTab(candidate.args[2], sessionId);
    }
    return browser.sessions.forgetClosedWindow(sessionId);
  }));
}
github kumabook / bebop / src / utils / sessions.js View on Github external
export async function restorePrevious() {
  const [session] = await fetch();
  if (session.tab) {
    return browser.sessions.restore(session.tab.sessionId);
  }
  return browser.sessions.restore(session.window.sessionId);
}