import { BookmarkOpener } from './BookmarkOpener' ; import BookmarkTreeNode = chrome . bookmarks . BookmarkTreeNode ; import { ChromeTranslator } from "./translator/ChromeTranslator" ; import { Translator } from "./Translator" ; export class Utils { static translator : Translator = new ChromeTranslator ( ) ; static getElementData ( element , key ) { const data = element . dataset [ key ] ; if ( typeof data === 'undefined' ) { throw new Error ( 'Element does not have data in key "' + key + '"' ) ; } return data ; } static openAllBookmarks ( folderId ) { chrome . bookmarks . getSubTree ( folderId , ( data ) => { BookmarkOpener . openAll ( data [ 0 ] , true ) ; window . close ( ) ; } ) ; } static nothing ( e ) { e . preventDefault ( ) ; e . stopPropagation ( ) ; e . stopImmediatePropagation ( ) ; return false ; } static setBrowserActionIcon ( icon ) { const iconPaths = { default : '/icons/bookmark48.png' , star : '/icons/black-star.png' , star_empty : '/icons/black-open-star.png' , white_star : '/icons/white-star.png' , white_star_empty : '/icons/white-open-star.png' } ; if ( ! iconPaths . hasOwnProperty ( icon ) ) { return ; } chrome . browserAction . setIcon ( { path : iconPaths [ icon ] } ) ; } static translateDocument ( document ) { const translatableElements = document . querySelectorAll ( '[data-i18n-key]' ) ; translatableElements . forEach ( ( translatableElement ) => { const key = translatableElement . getAttribute ( 'data-i18n-key' ) ; if ( null === key ) { return ; } const translation = Utils . translator . translate ( key ) ; if ( translation !== '' ) { translatableElement . innerHTML = translation ; } } ) ; } static addEventListenerMulti ( element , events , callback ) { events . split ( ' ' ) . forEach ( event => element . addEventListener ( event , callback , false ) ) ; } }