userChrome.js作成メモWiki http://w.atwiki.jp/fxext/ userChrome.js作成メモWiki ja 2021-09-02T10:25:49+09:00 1630545949 ボタンと右クリックメニュー https://w.atwiki.jp/fxext/pages/64.html *ツールバーボタンとボタン右クリック時のメニュー追加 #highlight(javascript){{ // ツールバーボタン作成 const { Services } = ChromeUtils.import('resource://gre/modules/Services.jsm'); const uri = Services.io.newURI('data:text/css;charset=utf-8,' + encodeURIComponent( ` #MyToolbarButton { list-style-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><circle fill="context-fill" cx="8" cy="8" r="6"/></svg>'); } ` ), null, null); const sss = Cc['@mozilla.org/content/style-sheet-service;1'].getService(Ci.nsIStyleSheetService); sss.loadAndRegisterSheet(uri, sss.AGENT_SHEET); const { CustomizableUI } = ChromeUtils.import('resource:///modules/CustomizableUI.jsm'); CustomizableUI.createWidget({ id: 'MyToolbarButton', defaultArea: CustomizableUI.AREA_NAVBAR, label: 'テストボタン', tooltiptext: 'テストボタン', onCommand(event) { event.target.ownerGlobal.alert('ボタン テスト'); }, }); // ツールバーのコンテキストメニュー追加 const ToolbarContextMenu = { handleE 2021-09-02T10:25:49+09:00 1630545949 utilityOverlay.js https://w.atwiki.jp/fxext/pages/63.html Firefox76時点での内容 chrome://browser/content/utilityOverlay.js browser.xhtmlやplaces.xhtmlなど、ほとんどの場所で読み込まれているutility #contents *変数 |AppConstants|resource://gre/modules/AppConstants.jsm| |Services|resource://gre/modules/Services.jsm| |XPCOMUtils|resource://gre/modules/XPCOMUtils.jsm| |>|| |AboutNewTab|resource:///modules/AboutNewTab.jsm| |BrowserWindowTracker|resource:///modules/BrowserWindowTracker.jsm| |ContextualIdentityService|resource://gre/modules/ContextualIdentityService.jsm| |ExtensionSettingsStore|resource://gre/modules/ExtensionSettingsStore.jsm| |PrivateBrowsingUtils|resource://gre/modules/PrivateBrowsingUtils.jsm| |ShellService|resource:///modules/ShellService.jsm| |>|| |ReferrerInfo|nsIReferrerInfoコンストラクタ| |BROWSER_NEW_TAB_URL|AboutNewTab.newTabURL あるいは "about:privatebrowsing"| |TAB_DROP_TYPE|"application/x-moz-tabbrowser-tab"| |gBidiUI|false| *関数 :isBlankPageURL(aURL)| :getTopWin(skipPopups)| 返り値:ブラウザWindow(windowtype=="navigator:browser") :d 2020-05-10T11:15:10+09:00 1589076910 Bookmarks.jsm https://w.atwiki.jp/fxext/pages/62.html Firefox75時点での内容 resource://gre/modules/Bookmarks.jsm jsmを直接importするより、PlacesUtils.bookmarksでアクセスする方が良い? #contents *ファイル冒頭の説明文の翻訳 ※ほぼ機械翻訳 * This module provides an asynchronous API for managing bookmarks. このモジュールは、ブックマークを管理するための非同期APIを提供します。 * Bookmarks are organized in a tree structure, and include URLs, folders and * separators. Multiple bookmarks for the same URL are allowed. ブックマークはツリー構造で編成されており、URL、フォルダ、セパレータが含まれています。 同じURLの複数のブックマークが許可されます。 * Note that if you are handling bookmarks operations in the UI, you should * not use this API directly, but rather use PlacesTransactions.jsm, so that * any operation is undo/redo-able. UIでブックマーク操作を処理している場合は、このAPIを直接使用するのではなく、[[PlacesTransactions.jsm]] を使用して、操作を元に戻したりやり直したりできるようにする必要があります。 * Each bookmark-item is represented by an object having the following * properties: * * - guid (string) * The globally unique identifier of the item. * - parentGuid (string) * The globally uniqu 2020-05-02T19:59:35+09:00 1588417175 PlacesUIUtils.jsm https://w.atwiki.jp/fxext/pages/61.html Firefox75時点での内容 resource:///modules/PlacesUIUtils.jsm #contents *メソッド :createFixedURI(aSpec)| :getFormattedString(key, params)| chrome://places/locale/places.properties の文字列取得 :getPluralString(aKey, aNumber, aParams)| :getString(key)| chrome://places/locale/places.properties の文字列取得 :showBookmarkDialog(aInfo, aParentWindow)| ブックマークダイアログを表示する :loadFavicon(browser, principal, pageURI, uri, expiration = 0, iconURI = null)| :getViewForNode(aNode)| 引数:DOMノード 返り値:[[Places]]ビュー 指定されたDOMノードの最も近い祖先のPlacesビューを返す :getControllerForCommand(win, command)| :updateCommands(win)| :doCommand(win, command)| :markPageAsTyped(aURL)| :markPageAsFollowedBookmark(aURL)| :markPageAsFollowedLink(aURL)| :async setCharsetForPage(url, charset, window)| :checkURLSecurity(aURINode, aWindow)| :canUserRemove(aNode)| :isFolderReadOnly(placesNode)| :openMultipleLinksInTabs(nodeOrNodes, event, view)| :openNodeWithEvent(aNode, aEvent)| :openNodeIn(aNode, aWhere, aView, aPrivate)| :gues 2020-05-01T18:18:20+09:00 1588324700 PlacesUtils.jsm https://w.atwiki.jp/fxext/pages/60.html Firefox76時点での内容 resource://gre/modules/PlacesUtils.jsm #contents *メソッド :isVirtualLeftPaneItem(guid)| 引数:GUID 返り値:真偽値 引数GUIDがライブラリの左ペインにある、履歴、ダウンロード、タグ、すべてのブックマークのどれかに当てはまるかどうか :asContainer(aNode)| コンテナのインターフェースを取得させる QueryInterface(Ci.[[nsINavHistoryContainerResultNode>https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsINavHistoryContainerResultNode]]) :asQuery(aNode)| クエリのインターフェースを取得させる QueryInterface(Ci.[[nsINavHistoryQueryResultNode>https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsINavHistoryQueryResultNode]]) :isValidGuid(guid)| 引数:文字列 返り値:真偽値 有効なGUIDかどうか :isValidGuidPrefix(guidPrefix)| 引数:文字列 返り値:真偽値 GUID用Prefixとして有効かどうか :generateGuidWithPrefix(prefix)| 引数:文字列 返り値:指定Prefix付GUID文字列 :toURI(url)| 引数:文字列 or URLオブジェクト 返り値:[[nsIURI>https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIURI]] :toPRTime(date)| 引数:Dateオブジェクト 返り値:マイクロ秒数(date*1000) :toDate(time)| 2020-05-09T14:20:39+09:00 1589001639 PlacesTransactions.jsm https://w.atwiki.jp/fxext/pages/59.html Firefox75時点での内容 resource://gre/modules/PlacesTransactions.jsm #contents *ファイル冒頭の説明文の翻訳 ※機械翻訳を少し手直ししただけなので、意味が分かりにくい所がある。 **概要 * Overview * -------- * This modules serves as the transactions manager for Places (hereinafter PTM). * It implements all the elementary transactions for its UI commands: creating * items, editing their various properties, and so forth. このモジュールは、[[Places]]のトランザクションマネージャー(以降、PTM)として機能します。 アイテムの作成、さまざまなプロパティの編集など、UIコマンドのすべての基本的なトランザクションを実装します。 * Note that since the effect of invoking a Places command is not limited to the * window in which it was performed (e.g. a folder created in the Library may be * the parent of a bookmark created in some browser window), PTM is a singleton. * It's therefore unnecessary to initialize PTM in any way apart importing this * module. [[Places]]コマンドの呼び出しの効果は、コマンドが実行されたウィンドウに限定されないため(たとえば、ライブラリで作成されたフォルダは、ブラウザウィンドウで作成されたブックマークの親である場合があります)、PTMはシングルトンです。 したがって、このモジュールのインポートとは別に、PTMを初期化する必要 2020-04-19T15:22:28+09:00 1587277348 ツールバーの追加 https://w.atwiki.jp/fxext/pages/58.html *ツールバーボックスの最後(ブックマークツールバーの下)にツールバーを追加 #highlight(javascript){{ try { // この部分がないと新規ウィンドウを開いた時の最初のタブが表示されない if(gBrowser.selectedBrowser.getAttribute('blank')) gBrowser.selectedBrowser.removeAttribute('blank'); } catch(e) {} // ツールバーのノードを作成 const df = window.MozXULElement.parseXULToFragment( ` <toolbar id="AdditionalToolbar" toolbarname="追加のツールバー" customizable="true" class="browser-toolbar customization-target" fullscreentoolbar="true" mode="icons" context="toolbar-context-menu" accesskey="A"> </toolbar> ` ); document.getElementById('navigator-toolbox').appendChild(df); // 作成したツールバーを登録 CustomizableUI.registerArea('AdditionalToolbar', { type: CustomizableUI.TYPE_TOOLBAR }); CustomizableUI.registerToolbarNode(document.getElementById('AdditionalToolbar')); // CSSを登録(カスタマイズ画面での見た目のため。なくてもいい) const uri = Services.io.newURI("data:text/css;charset=utf-8," + encodeURIComponent( ` #AdditionalToolbar[customizing] { outline: 1px dashed; ou 2020-01-11T23:01:02+09:00 1578751262 chrome.manifest登録 https://w.atwiki.jp/fxext/pages/57.html userChrome.jsでmanifestファイルの登録をする方法。 -[[Services.jsm - Mozilla | MDN>https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Services.jsm]] -[[nsIFile - Mozilla | MDN>https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIFile]] -[[nsIComponentRegistrar - Mozilla | MDN>https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIComponentRegistrar]] #highlight(javascript){{ const {interfaces: Ci, utils: Cu, manager: Cm} = Components; Cu.import("resource://gre/modules/Services.jsm"); let cmanifest = Services.dirsvc.get("UChrm", Ci.nsIFile); cmanifest.append("chrome.manifest"); Cm.QueryInterface(Ci.nsIComponentRegistrar).autoRegister(cmanifest); }} 2019-04-27T19:33:25+09:00 1556361205 自作jsmの読み込み https://w.atwiki.jp/fxext/pages/56.html userChrome.jsで自作のjsmを使う時の読み込み方。 *Firefox101以降 Firefox101からセキュリティ上の問題からChromeUtils.importでfileURIを読み込まなくなった。そのため、chromeやresourceパスを登録して読み込む必要がある。 #highlight(javascript){{ const uc = Services.dirsvc.get('UChrm', Ci.nsIFile); const protocolHandler = Services.io .getProtocolHandler("resource") .QueryInterface(Ci.nsIResProtocolHandler); const uri = Services.io.newFileURI(uc); protocolHandler.setSubstitution("ucjs", uri); const { TEST } = ChromeUtils.import('resource://ucjs/TEST.jsm'); }} -情報元 https://egg.5ch.net/test/read.cgi/software/1612892775/601 *Firefox100以前の方法 chromeパスが無くても、fileURIで読み込める。 -[[OSFile.jsm - Mozilla | MDN>https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/OSFile.jsm]] #highlight(javascript){{ Cu.import("resource://gre/modules/osfile.jsm"); Cu.import(OS.Path.toFileURI(OS.Constants.Path.profileDir) + "/chrome/~~.jsm"); }} そもそも、firefox60以上なら ES module が実装されているので、&del(){jsm ではなく ES module を使った方が良いかも。} -[[export - JavaScript | 2022-06-03T05:28:23+09:00 1654201703 userChrome.jsとは https://w.atwiki.jp/fxext/pages/55.html userChrome.jsとは、元々は、ユーザーが作成したFirefox本体を改造するためのスクリプトを読み込むためのレガシー拡張機能でした。Webページを改造するスクリプトを読み込むGreasemonkeyに対し、Firefox本体を改造するスクリプトを読み込むuserChrome.js、という風に対比すれば分かりやすいかもしれません。 正直、拡張機能の作り方を知っている人にとってはあまり意味のないものでした。小さな改造のために一々拡張機能を作るのが面倒という面倒臭がりな人か、作り方を知らない一般ユーザー向けでした。 しかし、レガシー拡張機能が完全に廃止されたFirefox57以降は、Firefox本体を改造する唯一の方法となっています。(userChrome.jsより先にレガシー拡張が無くなる未来が来るとか、思ってもみなかった…) もちろん、userChrome.js自体もレガシー拡張機能だったため、現在は元々のものとは姿を変えたものになっています。userChrome.js自体もスクリプトとなり、fx72現在では管理者が使う集中管理機能を利用して実行しています。 //userChrome.jsの導入は以下のサイトを見てください。 //-[[userChrome.js用スクリプト - wiki@nothing>http://wiki.nothing.sh/page?userChrome.js%CD%D1%A5%B9%A5%AF%A5%EA%A5%D7%A5%C8#m5c944e2]] *導入方法(2022/05/04時点) alice0775さんのGithubのuserChrome.jsページから必要なファイルをダウンロードして使用します。 [[GitHub - alice0775/userChrome.js>https://github.com/alice0775/userChrome.js]] +『config.js』、『config-prefs.js』、『userChrome.js』をダウンロードする ++『config.js』と『config-prefs.js』は現時点(2022/05/04)では 92 フォルダの install_folder フォルダ以下にあります ++『userChrome.js』は現時点(2022 2022-05-04T16:35:11+09:00 1651649711