diff --git a/pkg/extension/src/scripts/api.js b/pkg/extension/src/scripts/api.js new file mode 100644 index 000000000..73f5e416a --- /dev/null +++ b/pkg/extension/src/scripts/api.js @@ -0,0 +1,88 @@ +function updateLabelsCache(apiUrl, tab) { + const query = JSON.stringify({ + query: `query GetLabels { + labels { + ... on LabelsSuccess { + labels { + ...LabelFields + } + } + ... on LabelsError { + errorCodes + } + } + } + fragment LabelFields on Label { + id + name + color + description + createdAt + } + `, + }) + return fetch(apiUrl, { + method: 'POST', + redirect: 'follow', + credentials: 'include', + mode: 'cors', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: query, + }) + .then((response) => response.json()) + .then((data) => { + const result = data.data.labels.labels + return result + }) + .then((labels) => { + setStorage({ + labels: labels, + labelsLastUpdated: new Date().toISOString(), + }) + return labels + }) +} + +function updatePageTitle(apiUrl, pageId, title) { + console.log('updated title: ', apiUrl, pageId, title) + const mutation = JSON.stringify({ + query: `mutation UpdatePage($input: UpdatePageInput!) { + updatePage(input: $input) { + ... on UpdatePageSuccess { + updatedPage { + id + } + } + ... on UpdatePageError { + errorCodes + } + } + } + `, + variables: { + input: { + pageId, + title, + }, + }, + }) + + return fetch(apiUrl, { + method: 'POST', + redirect: 'follow', + credentials: 'include', + mode: 'cors', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: mutation, + }) + .then((response) => response.json()) + .then((data) => { + console.log('updated title: ', data) + }) +} diff --git a/pkg/extension/src/scripts/common.js b/pkg/extension/src/scripts/common.js new file mode 100644 index 000000000..2d3e17e6b --- /dev/null +++ b/pkg/extension/src/scripts/common.js @@ -0,0 +1,154 @@ +'use strict' + +window.browserApi = + (typeof chrome === 'object' && chrome && chrome.runtime && chrome) || + (typeof browser === 'object' && browser) || + {} // eslint-disable-line no-undef +window.browserActionApi = + browserApi.action || browserApi.browserAction || browserApi.pageAction +window.browserScriptingApi = browserApi.scripting || browserApi.tabs + +window.ENV_EXTENSION_ORIGIN = browserApi.runtime + .getURL('PATH/') + .replace('/PATH/', '') +window.ENV_IS_FIREFOX = ENV_EXTENSION_ORIGIN.startsWith('moz-extension://') +window.ENV_IS_EDGE = navigator.userAgent.toLowerCase().indexOf('edg') > -1 +window.ENV_DOES_NOT_SUPPORT_BLOB_URL_ACCESS = + /^((?!chrome|android).)*safari/i.test(navigator.userAgent) + +window.SELECTORS = { + CANONICAL_URL: ["head > link[rel='canonical']"], + TITLE: ["head > meta[property='og:title']"], +} + +window.ACTIONS = { + Ping: 'PING', + + ShowMessage: 'SHOW_MESSAGE', + GetContent: 'GET_CONTENT', + + AddIframeContent: 'ADD_IFRAME_CONTENT', + RefreshDarkMode: 'REFRESH_DARK_MODE', + GetAuthToken: 'GET_AUTH_TOKEN', + LabelCacheUpdated: 'LABEL_CACHE_UPDATED', + + ShowToolbar: 'SHOW_TOOLBAR', + UpdateStatus: 'UPDATE_STATUS', + + EditTitle: 'EDIT_TITLE', +} + +window.DONT_REMOVE_ELEMENTS = ['meta', 'script', 'title'] + +window.SAVE_URL_QUERY = `mutation SaveUrl ($input: SaveUrlInput!) { + saveUrl(input:$input){ + ... on SaveSuccess { + url + } + ... on SaveError { + errorCodes + } + } +}` + +window.SAVE_FILE_QUERY = `mutation SaveFile ($input: SaveFileInput!) { + saveFile(input:$input){ + ... on SaveSuccess { + url + } + ... on SaveError { + errorCodes + } + } +}` + +window.SAVE_PAGE_QUERY = `mutation SavePage ($input: SavePageInput!) { + savePage(input:$input){ + ... on SaveSuccess { + url + } + ... on SaveError { + errorCodes + } + } +}` + +window.CREATE_ARTICLE_QUERY = `mutation CreateArticle ($input: CreateArticleInput!){ + createArticle(input:$input){ + ... on CreateArticleSuccess{ + createdArticle{ + id + title + slug + hasContent + } + user { + id + profile { + id + username + } + } +} + ... on CreateArticleError{ + errorCodes + } +} +}` + +window.CREATE_ARTICLE_SAVING_REQUEST_QUERY = `mutation CreateArticleSavingRequest ($input: CreateArticleSavingRequestInput!){ + createArticleSavingRequest(input:$input){ + ... on CreateArticleSavingRequestSuccess{ + articleSavingRequest{ + id + } + } + ... on CreateArticleSavingRequestError{ + errorCodes + } + } +}` + +function handleBackendUrl(url) { + try { + const FORCE_CONTENT_FETCH_URLS = [ + // twitter status url regex + /twitter\.com\/(?:#!\/)?(\w+)\/status(?:es)?\/(\d+)(?:\/.*)?/, + /^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w-]+\?v=|embed\/|v\/)?)([\w-]+)(\S+)?$/, + ] + return FORCE_CONTENT_FETCH_URLS.some((regex) => regex.test(url)) + } catch (error) { + console.log('error checking url', url) + } + return false +} + +/* storage helper functions */ +function getStorage(keyOrKeys) { + return new Promise((resolve) => { + browserApi.storage.local.get(keyOrKeys || null, (result) => { + resolve(result || {}) + }) + }) +} + +function getStorageItem(singleKey) { + return new Promise((resolve) => { + browserApi.storage.local.get(singleKey, (result) => { + const finalResult = (result && result[singleKey]) || null + resolve(finalResult) + }) + }) +} + +function setStorage(itemsToSet) { + return new Promise((resolve) => { + browserApi.storage.local.set(itemsToSet, resolve) + }) +} + +function removeStorage(itemsToRemove) { + return new Promise((resolve) => { + browserApi.storage.local.remove(itemsToRemove, resolve) + }) +} diff --git a/pkg/extension/src/views/toast.html b/pkg/extension/src/views/toast.html new file mode 100644 index 000000000..f80b6fc75 --- /dev/null +++ b/pkg/extension/src/views/toast.html @@ -0,0 +1,284 @@ + +
+
+ + + + + + + + + + + + + + + + + + + + + + + +
+ +
+
+ + +
+
+
+
+ +
+ +
+ +
+
+ +
+ + + + +
+
\ No newline at end of file