将JavaScript代码转换为书签以方便访问

发布于 2025-01-25 17:42:26 字数 1347 浏览 4 评论 0 原文

我有这个代码试图用作书签。

fetch('https://hf.space/embed/Alifarsi/news_summarizer/+/api/predict/', { method: "POST", body: JSON.stringify({"data":[ "https://www.cp24.com/news/ontario-reports-481-new-covid-19-cases-1-death-1.5667950"]}), headers: { "Content-Type": "application/json" } }).then(function(response) { return response.json(); }).then(function(json_response){ console.log(json_response) })

我从:

/a>

通常书签以“ JavaScript:”关键字开头。我试图在字符串的开头添加,但没有获取预期的页面。


更新:

可能我没有解释我要实现的目标:

  1. drag&放下书签: https://codepen.io/shantanuo/penanuo/pen/pen/pen/pen/lywrabe

  2. 例如 https://pandas.pydata.orgg/docs/reference/reference/reference/reference/reference/reference/reference/reference/reference/reference/reference/reference/reference/reference/ pandas.read_csv.html

  3. 当您“活动”选项卡显示上述页面内容时,请单击“书签”。

您将看到此页面的列表。如果有效,我认为我可以使用API​​获取当前页面的摘要,以节省我阅读整篇文章的时间。但这似乎并不像上述过程那样容易。

I have this code that I am trying to use as a bookmarklet.

fetch('https://hf.space/embed/Alifarsi/news_summarizer/+/api/predict/', { method: "POST", body: JSON.stringify({"data":[ "https://www.cp24.com/news/ontario-reports-481-new-covid-19-cases-1-death-1.5667950"]}), headers: { "Content-Type": "application/json" } }).then(function(response) { return response.json(); }).then(function(json_response){ console.log(json_response) })

I got the code from:

https://hf.space/embed/Alifarsi/news_summarizer/api

Usually the bookmarklets start with "javascript:" keyword. I tried to added that at the start of the string, but it did not fetch the expected page.


Update:

May be I have not explained what I am trying to achieve:

  1. Drag & Drop the bookmarklet:
    https://codepen.io/shantanuo/pen/LYWRabE

  2. Visit a tech page for e.g.
    https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html

  3. Click on the bookmarklet when you the active tab is showing the contents of the page mentioned above.

You will see the stack-overflow questions where this page is referred. If this works, I thought I can get the summary of current page using the API that will save my time reading the entire article. But this does not seem to be as easy as the process mentioned above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红颜悴 2025-02-01 17:42:26

Chrome在新选项卡上使用 fetch()封锁书签。尝试在另一个网站上作为书签运行,例如 https://w3c.github.io 应该工作。

Chrome blocks bookmarklets using fetch() on a new Tab. Try running it as a bookmarklet on another site like for example https://w3c.github.io, and it should work.

不如归去 2025-02-01 17:42:26
JavaScript:指示符

领先的 JavaScript:对于浏览器识别书签是必不可少的。此 uri 的一部分被称为方案并确定接下来会发生什么。

“没有获取预期的页面”

JavaScript:通过URL执行,一般会在 chrome:// 页面上被阻止。 COM/CHOMIUM/CHOMIUM/BLOB/6895919A91D3E5418359582D82375CDD9A4C1BA/content/content/renderer/renderer/render_thread_impl.cc#l992“

  websecuritypolicy :: registerurlschemeasnotallowingjavascripturls(chrome_scheme);
 

新的选项卡页面是 chrome:// newtab/,因此书签不起作用。完整列表可以通过输入大约:关于。如果要使用书签,则必须将位置更改为 JavaScript:允许执行。

rentrentrentr

通过使用数据uri方案 data:以脚本标记生成一个以书签代码为脚本的html文件。

// must escape >,< for eval of Stackoverflow.com snippets
dataUriPrefix = 'data:text/html,\<body\>\<script\>\n'

function bookmarklet() {
  document.body.style.backgroundColor = 'lightsteelblue';
  fetch('https://hf.space/embed/Alifarsi/news_summarizer/+/api/predict/', {
      method: 'POST',
      body: JSON.stringify({
        'data': ['https://www.cp24.com/news/ontario-reports-481-new-covid-19-cases-1-death-1.5667950']
      }),
      headers: {
        'Content-Type': 'application/json'
      }
    })
    .then(response => response.json())
    .then(json => alert(JSON.stringify(json, null, 4)))
}

let anchor = document.getElementsByTagName('a')[0]
let pre = anchor.firstElementChild
pre.innerText = dataUriPrefix +
  bookmarklet.toString().slice(26, -2).replace(/^ {1,4}/gm, '') +
  '\n\</script\>'
let oneline = pre.innerText.replaceAll('\n', ' ')
anchor.setAttribute('href', oneline)
a {
  text-decoration: none;
}
<h5> Bookmarklet with link as text </h5>
<a title="Usable as bookmarklet"><pre></pre></a>

它将用此HTML存根替换您的当前页面,这可能很烦人。只需切换到 http(s): url可能会更容易。或将新的选项卡页面设置为via

javascript: indicator

The leading javascript: is essential for the browser to recognize a bookmarklet. This part of a URI is called scheme and decides what happens next.

"did not fetch the expected page"

javascript: execution via URL is blocked in general on all chrome:// pages in render_thread_impl.cc:992:

 WebSecurityPolicy::RegisterURLSchemeAsNotAllowingJavascriptURLs(chrome_scheme);

The new tab page is chrome://newtab/, so bookmarklets do not work there. The complete list can be seen by entering about:about. If you want to work the bookmarklet, you must change location to where javascript: execution is allowed.

A trick bookmarklet to switch location and run JS at the same time

By using the data URI scheme data: generate an in place html file with the bookmarklet code as script tag.

// must escape >,< for eval of Stackoverflow.com snippets
dataUriPrefix = 'data:text/html,\<body\>\<script\>\n'

function bookmarklet() {
  document.body.style.backgroundColor = 'lightsteelblue';
  fetch('https://hf.space/embed/Alifarsi/news_summarizer/+/api/predict/', {
      method: 'POST',
      body: JSON.stringify({
        'data': ['https://www.cp24.com/news/ontario-reports-481-new-covid-19-cases-1-death-1.5667950']
      }),
      headers: {
        'Content-Type': 'application/json'
      }
    })
    .then(response => response.json())
    .then(json => alert(JSON.stringify(json, null, 4)))
}

let anchor = document.getElementsByTagName('a')[0]
let pre = anchor.firstElementChild
pre.innerText = dataUriPrefix +
  bookmarklet.toString().slice(26, -2).replace(/^ {1,4}/gm, '') +
  '\n\</script\>'
let oneline = pre.innerText.replaceAll('\n', ' ')
anchor.setAttribute('href', oneline)
a {
  text-decoration: none;
}
<h5> Bookmarklet with link as text </h5>
<a title="Usable as bookmarklet"><pre></pre></a>

It will replace your current page with this html stub, which can be annoying. Might be easier to just switch to an http(s): URL. Or set the new tab page to such on via extension.

鹊巢 2025-02-01 17:42:26

从您的编辑中了解,您希望从 hf.space/embed/alifarsi/news_summarizer 获得当前页面的摘要。因此,代替 cp24.com 您必须将当前页面的URL传递给 location.href

<a href="
javascript:fetch('https://hf.space/embed/Alifarsi/news_summarizer/+/api/predict/', {
  method: 'POST',
  body: JSON.stringify({
    'data': [location.href]
  }),
  headers: {
    'Content-Type': 'application/json'
  }
})
.then(res => res.json())
.then(json => alert(json.data))
">Summarize the current page with Alifarsi/news_summarizer</a>

不过,它对复杂的API文档并没有太大帮助。通常,这些以主要想法在细节之前的方式写。为您的示例 pandas.pandas.read_cs.csv

将逗号分隔的值(CSV)文件读取到数据框中。

还可以选择将文件迭代或分解成块。

要学习基础知识,请更好地 search> search 比AI摘要文档。

As I understand from your edit, you want to get a summary of the current page from hf.space/embed/Alifarsi/news_summarizer. Therefore in place of cp24.com you must pass the current page's URL to it, stored in location.href:

<a href="
javascript:fetch('https://hf.space/embed/Alifarsi/news_summarizer/+/api/predict/', {
  method: 'POST',
  body: JSON.stringify({
    'data': [location.href]
  }),
  headers: {
    'Content-Type': 'application/json'
  }
})
.then(res => res.json())
.then(json => alert(json.data))
">Summarize the current page with Alifarsi/news_summarizer</a>

It won't help you much with complex API documentation though. Normally these are written in a way that the main idea comes before the details. For your example pandas.read_csv it would be

Read a comma-separated values (csv) file into DataFrame.

Also supports optionally iterating or breaking of the file into chunks.

To learn the basics, better search for a tutorial than an AI summary of the docs.

梦过后 2025-02-01 17:42:26

您需要将代码放在IIFE中 -

(function () {
    // your code here
})()

因此您的代码应该是 -

javascript: (() => { fetch('https://hf.space/embed/Alifarsi/news_summarizer/+/api/predict/', { method: "POST", body: JSON.stringify({"data":[ "https://www.cp24.com/news/ontario-reports-481-new-covid-19-cases-1-death-1.5667950"]}), headers: { "Content-Type": "application/json" } }).then(function(response) { return response.json(); }).then(function(json_response){ console.log(json_response) }) })()

我已经在浏览器中对其进行了测试。它可以很好地记录控制台中的响应数据。您只需将代码像下面的图像一样。它应该很好。

You need to put you code inside an IIFE like this -

(function () {
    // your code here
})()

So your code should be -

javascript: (() => { fetch('https://hf.space/embed/Alifarsi/news_summarizer/+/api/predict/', { method: "POST", body: JSON.stringify({"data":[ "https://www.cp24.com/news/ontario-reports-481-new-covid-19-cases-1-death-1.5667950"]}), headers: { "Content-Type": "application/json" } }).then(function(response) { return response.json(); }).then(function(json_response){ console.log(json_response) }) })()

I have tested it in my browser. It logs the response data in the console just fine. You can just take the code do it like the image below. It should work just fine.

Like this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文