Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Quest Pinner
- // @version 0.9
- // @description Dodatek przypinający quest do chatu w grze
- // @author Akechi
- // @match http://*.margonem.pl/
- // @grant none
- // ==/UserScript==
- ((Engine, API, $) => {
- 'use strict';
- let pinnedQuests = {};
- const lsName = "addon_PinQuests/list";
- const chatQuests = $(`<div class="pinned-quests" />`);
- $("body").append(`
- <style>
- .pin-quest-bck {
- background: url(../img/gui/buttony.png?v=6) no-repeat -497px -396px;
- width:16px;
- height:16px;
- top: 4px;
- left: 4px;
- position: absolute;
- }
- .pinned-quests {
- margin-bottom: .7em;
- position: relative;
- z-index: 0;
- order:2;
- }
- .pinned-quests > p {
- border-bottom: 1px solid #526543;
- padding: .5em .4em .3em;
- box-shadow: 0px 1px 4px rgba(0, 0, 0, 0.75);
- position: relative;
- }
- .pinned-quests > p:before {
- content: '';
- display: block;
- position: absolute;
- z-index: -1;
- background-color: rgba(95, 0, 200, 0.25);
- width: 100%;
- height: 100%;
- top: 0;
- left: 0;
- }
- .pinned-quests b {
- display:block;
- color: #d0ff48;
- margin-bottom: .2em;
- }
- .pinned-quests .q_kill {
- display: block;
- }
- .pinned-quests .q_kill > .red {
- color: #f16462;
- }
- .left-column .messages-wrapper .scroll-pane {
- display: flex;
- flex-direction: column;
- }
- .left-column .messages-wrapper .scroll-pane > .chat-message {
- order:1;
- }
- </style>
- `);
- this.detectQuestsWindowInit = () => {
- const self = this;
- let questsObj;
- Object.defineProperty(Engine, 'quests', {
- set(value) {
- questsObj = value;
- if(value !== false) {
- self.hookQuestBox();
- }
- },
- get () { return questsObj; }
- });
- }
- this.hookQuestBox = () => {
- const _showDetails = Engine.quests.showDetails;
- Engine.quests.showDetails = (...args) => {
- this.addPinButton(args);
- return _showDetails.apply(this, args);
- }
- }
- this.addPinButton = (quest) => {
- const [buttons, , id] = quest;
- const btn = API.Templates.get('button').addClass("small").append(`<div class="pin-quest-bck" />`);
- btn.find(".label").css("visibility", "hidden").html("0");
- this.updateButton(id, btn);
- buttons.push(
- btn.on("click", () => {
- this.pinQuest(id);
- this.updateButton(id, btn);
- })
- );
- }
- this.updateButton = (id, button) => { //call after change in Storage
- if(this.isPinned(id)) {
- button.removeClass("green").addClass("red").tip("Odepnij questa");
- } else {
- button.removeClass("red").addClass("green").tip("Przypnij questa");
- }
- }
- this.isPinned = id => (pinnedQuests[id] === undefined ? false : pinnedQuests[id]);
- this.pinQuest = (id) => {
- pinnedQuests[id] = !this.isPinned(id);
- API.Storage.set(lsName, pinnedQuests);
- this.refreshQuests();
- }
- this.getPinnedQuests = () => {
- return Object.keys(pinnedQuests).filter(id => pinnedQuests[id] ? true : false);
- }
- this.getQuests = () => {
- return new Promise((resolve, reject) => {
- $.post(`engine?t=quests${Engine.communication.prepareUrl()}`, (e) => {
- if (e.quests !== undefined)
- resolve(e.quests);
- else
- reject(`There is not information about quests`);
- });
- });
- }
- const $window = $(window);
- this.refreshQuests = () => {
- this.getQuests()
- .then(quests => {
- this.reWriteQuests(quests);
- }).then(()=>{
- Engine.chat.setScrollOnBottom();
- $window.resize();
- });
- }
- this.reWriteQuests = (quests) => {
- const pinnedQuests = this.getPinnedQuests();
- const startedQuests = $(quests);
- chatQuests.empty();
- for(const id of pinnedQuests) {
- for (const stertedQuest of startedQuests) {
- const temp = $(stertedQuest);
- if(id == temp.data('quest-id')) {
- const title = stertedQuest.firstChild.textContent;
- $(stertedQuest.firstChild).remove();
- this.colorKillWord(stertedQuest);
- chatQuests.append(
- temp.prepend(`<b>${title}</b>`).addClass("chat-message")
- );
- break;
- }
- }
- }
- }
- this.colorKillWord = function (obj) {
- const str = $(obj).html();
- const newStr = str.replace(/Zabij:/gi, '<span class="red">Zabij:</span>');
- $(obj).html(newStr);
- };
- const chat = $(".left-column .messages-wrapper .scroll-pane");
- let inited = false;
- this.watchChatChanges = () => {
- const observer = new MutationObserver(() => {
- if(!chat.find(".pinned-quests").length) {
- if(!inited) {
- inited = true;
- this.refreshQuests();
- }
- chat.append(chatQuests);
- }
- });
- observer.observe(chat.get(0), {childList: true});
- }
- this.detectEndBattle = () => {
- const _detectEndBattle = Engine.communication.dispatcher.on_f;
- Engine.communication.dispatcher.on_f = (data) => {
- if (data.close !== undefined && data.close == 1) {
- this.refreshQuests();
- }
- return _detectEndBattle.apply(this, [data]);
- }
- }
- this.detectNewDialogues = () => {
- const _detectDialogues = Engine.communication.dispatcher.on_d;
- Engine.communication.dispatcher.on_d = (data, all_data) => {
- if(!(data[0] == 4 && all_data.f !== undefined)) {
- this.refreshQuests();
- }
- return _detectDialogues.apply(this, [data]);
- }
- }
- this.init = () => {
- pinnedQuests = API.Storage.get(lsName) || {};
- this.detectQuestsWindowInit();
- this.watchChatChanges();
- this.detectEndBattle();
- this.detectNewDialogues();
- }
- this.init();
- })(window.Engine, window.API, window.$);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement