最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "Uncaught SyntaxError: Unexpected end of input" When converting to bookmarklet - Stack Overflow

programmeradmin3浏览0评论

I have a script that works fine just as plain JS, but when I use / to convert it to a bookmarklet and try to run it I get Uncaught SyntaxError: Unexpected end of input

I have used this website before to convert to Bookmarklets without any errors. If I paste the un-converted code into the console and call the function it runs fine.

un-converted:

function copy() {
  var number = document.getElementById('sys_readonly.rm_story.number').value,
      shortDescription = document.getElementById('rm_story.short_description').value,

      d = new Date(),
      year = d.getFullYear(),

      //The '0' and slice makes sure the numbers are at least 2 characters long. The '+1' is becuase it starts at January == 0.
      month = ('0' + (d.getMonth() + 1)).slice(-2),
      day = ('0' + d.getDate()).slice(-2),

      name = (year) + (month) + (day) + ' - ' + number + ' - ' + shortDescription;
      //Cut off everything past 80 characters
      if(name.length > 80) name = name.substring(0,80);

  // Pushes the String into the "Update Sets" field
  document.getElementById('rm_story.u_update_set').value = name;
}

Here it is converted.

javascript:(function()%7Bfunction%20copy()%20%7Bvar%20number%20%3D%20document.getElementById('sys_readonly.rm_story.number').value%2CshortDescription%20%3D%20document.getElementById('rm_story.short_description').value%2Cd%20%3D%20new%20Date()%2Cyear%20%3D%20d.getFullYear()%2C%2F%2FThe%20'0'%20and%20slice%20makes%20sure%20the%20numbers%20are%20at%20least%202%20characters%20long.%20The%20'%2B1'%20is%20becuase%20it%20starts%20at%20January%20%3D%3D%200.month%20%3D%20('0'%20%2B%20(d.getMonth()%20%2B%201)).slice(-2)%2Cday%20%3D%20('0'%20%2B%20d.getDate()).slice(-2)%2Cname%20%3D%20(year)%20%2B%20(month)%20%2B%20(day)%20%2B%20'%20-%20'%20%2B%20number%20%2B%20'%20-%20'%20%2B%20shortDescription%3B%2F%2FCut%20off%20everything%20past%2080%20charactersif(name.length%20%3E%2080)%20name%20%3D%20name.substring(0%2C80)%3B%2F%2F%20Pushes%20the%20String%20into%20the%20%22Update%20Sets%22%20fielddocument.getElementById('rm_story.u_update_set').value%20%3D%20name%3B%7D%7D)()

I have a script that works fine just as plain JS, but when I use https://mrcoles./bookmarklet/ to convert it to a bookmarklet and try to run it I get Uncaught SyntaxError: Unexpected end of input

I have used this website before to convert to Bookmarklets without any errors. If I paste the un-converted code into the console and call the function it runs fine.

un-converted:

function copy() {
  var number = document.getElementById('sys_readonly.rm_story.number').value,
      shortDescription = document.getElementById('rm_story.short_description').value,

      d = new Date(),
      year = d.getFullYear(),

      //The '0' and slice makes sure the numbers are at least 2 characters long. The '+1' is becuase it starts at January == 0.
      month = ('0' + (d.getMonth() + 1)).slice(-2),
      day = ('0' + d.getDate()).slice(-2),

      name = (year) + (month) + (day) + ' - ' + number + ' - ' + shortDescription;
      //Cut off everything past 80 characters
      if(name.length > 80) name = name.substring(0,80);

  // Pushes the String into the "Update Sets" field
  document.getElementById('rm_story.u_update_set').value = name;
}

Here it is converted.

javascript:(function()%7Bfunction%20copy()%20%7Bvar%20number%20%3D%20document.getElementById('sys_readonly.rm_story.number').value%2CshortDescription%20%3D%20document.getElementById('rm_story.short_description').value%2Cd%20%3D%20new%20Date()%2Cyear%20%3D%20d.getFullYear()%2C%2F%2FThe%20'0'%20and%20slice%20makes%20sure%20the%20numbers%20are%20at%20least%202%20characters%20long.%20The%20'%2B1'%20is%20becuase%20it%20starts%20at%20January%20%3D%3D%200.month%20%3D%20('0'%20%2B%20(d.getMonth()%20%2B%201)).slice(-2)%2Cday%20%3D%20('0'%20%2B%20d.getDate()).slice(-2)%2Cname%20%3D%20(year)%20%2B%20(month)%20%2B%20(day)%20%2B%20'%20-%20'%20%2B%20number%20%2B%20'%20-%20'%20%2B%20shortDescription%3B%2F%2FCut%20off%20everything%20past%2080%20charactersif(name.length%20%3E%2080)%20name%20%3D%20name.substring(0%2C80)%3B%2F%2F%20Pushes%20the%20String%20into%20the%20%22Update%20Sets%22%20fielddocument.getElementById('rm_story.u_update_set').value%20%3D%20name%3B%7D%7D)()
Share Improve this question asked Jun 26, 2018 at 19:21 Cody SmithCody Smith 16710 bronze badges 3
  • Should the code be wrapped in a function? – Musa Commented Jun 26, 2018 at 19:26
  • I don't think that should matter. I just tested it without being in a function and it still gives me the same error. – Cody Smith Commented Jun 26, 2018 at 19:28
  • 2 Try removing the ments – Musa Commented Jun 26, 2018 at 19:28
Add a ment  | 

1 Answer 1

Reset to default 11

Updated the ments to /**/ from //

I think the conversion was catching it as one long ment and this solved it.

function copy() {
  var number = document.getElementById('sys_readonly.rm_story.number').value,
      shortDescription = document.getElementById('rm_story.short_description').value,

      d = new Date(),
      year = d.getFullYear(),

      /*The '0' and slice makes sure the numbers are at least 2 characters long. The '+1' is becuase it starts at January == 0.*/
      month = ('0' + (d.getMonth() + 1)).slice(-2),
      day = ('0' + d.getDate()).slice(-2),

      name = (year) + (month) + (day) + ' - ' + number + ' - ' + shortDescription;
      /*Cut off everything past 80 characters*/
      if(name.length > 80) name = name.substring(0,80);

  /*Pushes the String into the "Update Sets" field*/
  document.getElementById('rm_story.u_update_set').value = name;
  console.log(name);
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论