【问题标题】:Chrome Bookmarklet to Check URL用于检查 URL 的 Chrome 书签
【发布时间】:2020-09-04 15:42:04
【问题描述】:

我正在创建一个 chrome javascript 书签,参考:

this question

我的代码:

javascript:(function NoOverrideAction(t){this.currentWindow=t,this.urlMatchPattern=/https:\/\/.*visual\.force\.com\/apex\/.*/,this.urlMatchPattern1=/https:\/\/.*visualforce\.com\/apex\/.*/,null!=this.currentWindow.location.toString().match(this.urlMatchPattern)||null!=this.currentWindow.location.toString().match(this.urlMatchPattern1)?this.isPageValid=!0:this.isPageValid=!1,this.recordId} NoOverrideAction.prototype={getId:function(){this.currentWindow.location.search.substr(1).split("&").forEach(function(t){var i=t.split("=");"id"==i[0]&&(this.recordId=i[1])},this)},run:function(){this.getId(),console.log(this),this.isPageValid&&void 0!==this.recordId&&(this.currentWindow.location.href="https://"+this.currentWindow.location.hostname+"/"+this.recordId+"?nooverride=1")}};var noAction=new NoOverrideAction(window);noAction.run();)();

但是,我收到以下错误:

Uncaught SyntaxError: Unexpected identifier

这在控制台中运行良好,但不能作为书签。

我正在尝试验证当前页面的 URL 并替换该 URL

【问题讨论】:

    标签: javascript google-chrome bookmarklet


    【解决方案1】:

    第一部分。

    js-code 可能在控制台中工作但不能作为书签工作的几个原因

    1 -您没有使用既定的制作书签的方法,即 IIFE 函数。

    javascript:(function(){
    alert('hi');
    /*code here*/
    })();
    

    2 -如果你确实使用这种结构,那么cmets可能会出现问题。

    使用/*multi-line comment*/而不是//single-line comment(因为书签是单行的,所以你不能关闭你的评论)

    3 - 分号;的一些问题。

    不要混淆语句(需要分号)和赋值(不需要它们)。

    控制台有它的“自动分号插入”,但是对于单行小书签来说这是个问题。

    您应该在这里阅读更多内容:https://stackoverflow.com/a/2717956/5410914

    第二部分

    由于没有要测试的 HTML 代码,因此可能很难检查。此外,您可以通过将其设置为多行使其更易于阅读(当您将其粘贴为书签时,它仍可用作书签)。但无论如何。

    它可能不起作用的主要原因是您没有使用 IIFE 格式(立即调用函数表达式)。

    您的代码有function NoOverrideAction(t),而且最后还有};var noAction=new NoOverrideAction(window);noAction.run();)();。它不是 IIFE。

    阅读更多:https://developer.mozilla.org/ru/docs/Glossary/IIFE

    要自己调试书签,因为没有提供 HTML,您可以尝试使您的代码更简单(即使一开始使用alert('hi');,并验证它无法启动的原因),然后使其更简单一旦你发现它有效,就会变得复杂。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多