簡體   English   中英

用於檢查 URL 的 Chrome 書簽

[英]Chrome Bookmarklet to Check URL

我正在創建一個 chrome javascript 書簽,參考:

這個問題

我的代碼:

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

第一部分。

js-code 可能在控制台中工作但不能作為書簽工作的幾個原因

1 -您不使用制作書簽的既定方式,即 IIFE 功能。

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

2 -如果您確實使用此結構,則注釋可能存在問題。

使用/*multi-line comment*/而不是//single-line comment (因為書簽是單行的,所以你不能關閉你的評論)

3 -分號的一些問題; .

不要混淆語句(需要分號)和賦值(不需要它們)。

控制台有它的“自動分號插入”,但對於單行小書簽來說這是一個問題。

您應該在這里閱讀更多內容: https://stackoverflow.com/a/2717956/5410914

第二部分

由於沒有要測試的 HTML 代碼,因此可能很難檢查。 此外,您可以通過將其設置為多行使其更易於閱讀(當您將其粘貼為書簽時,它仍可用作書簽)。 但不管怎么說。

它可能不起作用的主要原因是您不使用IIFE 格式(立即調用 Function 表達式)。

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

閱讀更多: https://developer.mozilla.org/ru/docs/Glossary/IIFE

要自己調試小書簽,因為沒有提供 HTML,您可能會嘗試使您的代碼更簡單(即使首先使用alert('hi'); ,並驗證它不會啟動的原因)然后制作它一旦你發現它有效,就會變得更加復雜。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM