用 javascript 制作书签

发布于 2024-11-16 01:15:18 字数 756 浏览 2 评论 0原文

我正在尝试为我的网站制作一个书签。

我制作了一个 php 页面,当发送 GET 例如 www.website.com/index.html?a=banana 时,它将返回 echo 'stand' ;

现在我正在尝试制作一个书签,当我按下它时,它会: 对 php 页面执行 GET,然后在一个小弹出窗口中显示回显给用户的所有内容,该弹出窗口在 3 秒后消失。

我该怎么做?

Instapaper 书签可以做到...

javascript:
function%20iprl5(){
  var%20d=document,z=d.createElement('scr'+'ipt'),b=d.body,l=d.location;
  try{
    if(!b)
      throw(0);
    d.title='(Saving...)%20'+d.title;
    z.setAttribute('src',l.protocol+'//www.instapaper.com/j/deyNbbpjuSei?u='+encodeURIComponent(l.href)+'&t='+(new%20Date().getTime()));
    b.appendChild(z);
  }
  catch(e){
    alert('Please%20wait%20until%20the%20page%20has%20loaded.');
  }
}
iprl5();
void(0)

I am trying to make a bookmarklet for my website.

I have made a php page that when sent a GET for example www.website.com/index.html?a=banana it will return echo 'stand';

Now I am trying to make a bookmarklet that when I press it will:
Do the GET to the php page, then display whatever the echo was back to the user in a small popup that disapears after 3 seconds.

How can I do this?

Instapaper bookmarklet does it...

javascript:
function%20iprl5(){
  var%20d=document,z=d.createElement('scr'+'ipt'),b=d.body,l=d.location;
  try{
    if(!b)
      throw(0);
    d.title='(Saving...)%20'+d.title;
    z.setAttribute('src',l.protocol+'//www.instapaper.com/j/deyNbbpjuSei?u='+encodeURIComponent(l.href)+'&t='+(new%20Date().getTime()));
    b.appendChild(z);
  }
  catch(e){
    alert('Please%20wait%20until%20the%20page%20has%20loaded.');
  }
}
iprl5();
void(0)

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

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

发布评论

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

评论(1

缱倦旧时光 2024-11-23 01:15:18

小书签是一段在您所在页面范围内运行的 JavaScript。

它并不意味着只调用任意 URL,也不能这样做,因为这会违反同源实践。如果您的 PHP 可以返回 JSONP,那么您可以编写一个书签,该书签将在调用您返回的脚本的页面上插入一个脚本 - 这也将能够显示一个弹出窗口,

javascript:
function%20iprl5(){
  var%20d=document; // shorten document object
  var z=d.createElement('scr'+'ipt'); // create a script tag
  var b=d.body; // get document.body
  var l=d.location; // get document.location - I would get document.URL instead
  try{
    if(!b) throw(0); // if there is no body object available
    d.title='(Saving...)%20'+d.title; // set document.title
    z.setAttribute('src',l.protocol+'//www.yourserver.com/test.php?u='+encodeURIComponent(l.href)+'&time='+(new%20Date().getTime())); // create the script url
    b.appendChild(z); // append it to the body - I would append to head myself
  }
  catch(e){ // give an error
    alert('Please%20wait%20until%20the%20page%20has%20loaded.');
  }
}
iprl5(); // call it
void(0); // make sure it does not return a value to the window

粘贴此 view-source:http:// www.instapaper.com/j/deyNbbpjuSei?u=http://www.stackoverflow.com 进入地址栏,查看作为您所在页面的一部分返回的内容量

A bookmarklet is a piece of javascript that runs in the scope of the page you are on.

It is not meant to just call arbitrary urls and cannot do so since that would be in violation of same origin practice. If your PHP can return JSONP, then you can write a bookmarklet that will insert a script on the page that envokes the script you return - that would be able to display a popup too

javascript:
function%20iprl5(){
  var%20d=document; // shorten document object
  var z=d.createElement('scr'+'ipt'); // create a script tag
  var b=d.body; // get document.body
  var l=d.location; // get document.location - I would get document.URL instead
  try{
    if(!b) throw(0); // if there is no body object available
    d.title='(Saving...)%20'+d.title; // set document.title
    z.setAttribute('src',l.protocol+'//www.yourserver.com/test.php?u='+encodeURIComponent(l.href)+'&time='+(new%20Date().getTime())); // create the script url
    b.appendChild(z); // append it to the body - I would append to head myself
  }
  catch(e){ // give an error
    alert('Please%20wait%20until%20the%20page%20has%20loaded.');
  }
}
iprl5(); // call it
void(0); // make sure it does not return a value to the window

paste this view-source:http://www.instapaper.com/j/deyNbbpjuSei?u=http://www.stackoverflow.com into the location bar to see the amount of stuff returned as part of the page you are on

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