如何制作一个JavaScript书签来添加到URL的一部分?

How do I make a JavaScript bookmarklet to add to part of the URL?u

本文关键字:添加 URL 一部分 书签 JavaScript 何制作 一个      更新时间:2023-09-26

我正在尝试制作一个JavaScript书签来转到我的数学书的下一页。URL是website.com/book_year_pagenumber.pdf,所以第500页应该是website.com/book_year_0500.pdf

我需要一些东西添加1到500使其website.com/book_year_0501.pdf,但它必须在任何页面上工作,如从第200页到201页或第573页到574页。有人知道怎么做吗?

编辑:也许这将帮助。我想让它变成http://my.hrw.com/math06_07/student/pdf/english/alg2/alg2_07_0500.pdf成"http://my.hrw.com/math06_07/student/pdf/english/alg2/alg2_07_0501.pdf"

javascript:(function(){var url=location.href.split('book_year_');var currentPage=+(url[1].split('.')[0])+1;while(currentPage.toString().length<4){ currentPage='0'+currentPage;}location.href=url[0]+('book_year_')+currentPage+'.pdf';})()不行吗?

略读:

// declare that this bookmarklet is a js script
javascript:(function(){
  // parse the current url
  var url = location.href.split('book_year_');
  // get the page number, transform it to a Number and increment it
  var currentPage = +(url[1].split('.')[0])+1;
  // add the leading zeroes
  while(currentPage.toString().length<4){ currentPage = '0' + currentPage;}
  // set the new url      
  location.href= url[0]+('book_year_')+currentPage+'.pdf';
// call the function
})()