Skip to content

Instantly share code, notes, and snippets.

@steelydylan
Last active June 21, 2017 17:35
Show Gist options
  • Save steelydylan/ac3c632e7eb94453032b1c0155110580 to your computer and use it in GitHub Desktop.
Save steelydylan/ac3c632e7eb94453032b1c0155110580 to your computer and use it in GitHub Desktop.
Pocket数を取得するためのJavaScript
async function get_pocket_count(url) {
const pocketUrl = encodeURIComponent(`https://widgets.getpocket.com/v1/button?v=1&count=horizontal&url=${url}&src=${url}`);
const response = await fetch(`http://query.yahooapis.com/v1/public/yql?q=select * from%20xml%20where%20url%3D%22${pocketUrl}%22&format=xml`)
const str = await response.text();
const doc = await new window.DOMParser().parseFromString(str, "text/xml")
return doc.querySelector('#cnt').innerHTML;
}
@steelydylan
Copy link
Author

こんな感じで取得できます。

get_pocket_count('https://appleple.github.io/SmartPhoto/').then(data => {console.log(data)});

@steelydylan
Copy link
Author

steelydylan commented Jun 21, 2017

chromeのpopupとの組み合わせ

chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
  const url = tabs[0].url;
  get_pocket_count(url).then((data) => {
    document.querySelector('#count').innerHTML = data;
  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment